2011-03-20 34 views
4

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

这里是我使用找到鼠标的位置的方法(I实例化在Game1类的开始的“MouseState”类):

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

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

回答

15

加载一个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

内容。加载<>不会扩展,对吧? – 2014-05-14 19:31:03

+0

正确,它不需要扩展。固定。谢谢。 – pek 2014-05-14 20:01:46

1

,你还可以使用图形用户界面和手动加载一个窗口光标来替换默认光标