在 XNA/C# 中添加自定义光标?

发布于 2024-10-25 07:53:52 字数 375 浏览 0 评论 0原文

我目前正在 XNA 中开发一款游戏。我想在游戏中添加一个光标(不是标准的 Windows 光标)。我已经将精灵添加到我的内容文件夹中。我有一种查找鼠标位置的方法,但我不知道应该如何在窗口中显示光标。

这是我用来查找鼠标位置的方法(我在 Game1 类的开头实例化了一个“MouseState”类):

public int[] getCursorPos()
    {
        cursorX = mouseState.X;
        cursorY = mouseState.Y;

        int[] mousePos = new int[] {cursorX, cursorY};
        return mousePos;
    }

I am currently developing a game in XNA. I would like to add a cursor (not the standard Windows one)to the game. I have already added the sprite to my contents folder. I have a method for finding the position of the mouse, but I don't know how I should go about displaying the cursor in the window.

Here is the method I am using to find the position of the mouse (I instantiated a "MouseState" class in the beginning of the Game1 class):

public int[] getCursorPos()
    {
        cursorX = mouseState.X;
        cursorY = mouseState.Y;

        int[] mousePos = new int[] {cursorX, cursorY};
        return mousePos;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈甜 2024-11-01 07:53:52

为光标图像加载一个Texture2D并简单地绘制它。

class Game1 : Game 
{
  private SpriteBatch spriteBatch;

  private Texture2D cursorTex;
  private Vector2 cursorPos;


  protected override void LoadContent() 
  {
    spriteBatch = new SpriteBatch(GraphicsDevice);
    cursorTex = content.Load<Texture2D>("cursor");
  }

  protected override Update(GameTime gameTime() {
    cursorPos = new Vector2(mouseState.X, mouseState.Y);
  }

  protected override void Draw(GameTime gameTime)
  {
    spriteBatch.Begin();
    spriteBatch.Draw(cursorTex, cursorPos, Color.White);
    spriteBatch.End();
  }
}

Load a Texture2D for the cursor image and simply draw it.

class Game1 : Game 
{
  private SpriteBatch spriteBatch;

  private Texture2D cursorTex;
  private Vector2 cursorPos;


  protected override void LoadContent() 
  {
    spriteBatch = new SpriteBatch(GraphicsDevice);
    cursorTex = content.Load<Texture2D>("cursor");
  }

  protected override Update(GameTime gameTime() {
    cursorPos = new Vector2(mouseState.X, mouseState.Y);
  }

  protected override void Draw(GameTime gameTime)
  {
    spriteBatch.Begin();
    spriteBatch.Draw(cursorTex, cursorPos, Color.White);
    spriteBatch.End();
  }
}
风筝有风,海豚有海 2024-11-01 07:53:52

您还可以使用 GUI 并手动加载 Windows 光标来替换默认光标

you can also use GUI and manually load a windows cursor to replace the default cursor

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文