添加在XNA / C#自定义光标? [英] Adding a custom cursor in XNA/C#?
本文介绍了添加在XNA / C#自定义光标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在开发一个游戏XNA。我想补充一个光标(不是标准的Windows一个)的游戏。我已经加了雪碧我的内容的文件夹。我有发现鼠标的位置的方法,但我不知道我应该去显示光标在窗口中。
下面是我使用找到鼠标的位置的方法(我实例在Game1类的开头MouseState级):
公众诠释[] getCursorPos()
{
cursorX = mouseState.X;
粗略= mouseState.Y;
INT [] mousePos结构=新INT [] {cursorX,走马};
返回mousePos结构;
}
解决方案
加载一个Texture2D游标形象,简单地画它。
类的Game1:游戏
{
私人SpriteBatch spriteBatch;
私人的Texture2D cursorTex;
私人Vector2 cursorPos;
保护覆盖无效LoadContent()
{
spriteBatch =新SpriteBatch(GraphicsDevice的);
cursorTex = content.Load<的Texture2D>(光标);
}
保护覆盖更新(GameTime gameTime(){
cursorPos =新Vector2(mouseState.X,mouseState.Y);
}
保护覆盖无效抽奖(GameTime gameTime)
{
spriteBatch.Begin();
spriteBatch.Draw(cursorTex,cursorPos,Color.White);
spriteBatch.End();
}
}
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;
}
解决方案
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();
}
}
这篇关于添加在XNA / C#自定义光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文