我这里有一些简单的代码来获取鼠标位置并绘制它:
class Pointer
{
MouseState currPointerState;
Vector2 currPointerPos;
public Pointer()
{
}
public void Update()
{
currPointerState = Mouse.GetState();
currPointerPos.X = currPointerState.X;
currPointerPos.Y = currPointerState.Y;
}
public void Draw(SpriteBatch spriteBatch, Texture2D pointerTexture)
{
spriteBatch.Draw(pointerTexture, currPointerPos, Color.White);
}
}
然后在我的主游戏文件中:
protected override void Update(GameTime gameTime)
{
pointer.Update();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
pointer.Draw(spriteBatch, pointerTexture);
menuManager.Draw(spriteBatch, menu_bar);
spriteBatch.End();
base.Draw(gameTime);
}
游戏目前在窗口中运行,但是鼠标距离实际鼠标位置约 500 像素,距离实际鼠标位置远 100 像素。
这发生在我添加以下代码之后:(菜单绘图类)
enum MenuState
{
mainMenu,
playing,
options
};
class MenuManager : Game1
{
MenuState menuState = MenuState.mainMenu;
Vector2 button1 = Vector2.Zero;
public void Draw(SpriteBatch spriteBatch, Texture2D menuBar)
{
switch (menuState)
{
case MenuState.mainMenu:
spriteBatch.Draw(menuBar, button1, Color.White);
break;
}
}
}
}
知道为什么鼠标位置可能已更改吗?