【问题标题】:Adding a custom cursor in XNA/C#?在 XNA/C# 中添加自定义光标?
【发布时间】:2011-03-20 21:31:08
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: c# .net xna mouse-cursor


    【解决方案1】:

    为光标图像加载一个 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();
      }
    }
    

    【讨论】:

    • 正确,它不需要扩展。固定的。谢谢。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      • 2021-10-15
      相关资源
      最近更新 更多