C#XNA鼠标位置

Foo*_*hip 5 c# xna

我在XNA中遇到了鼠标坐标的一些问题 - 0x0在屏幕的左上角任意靠近(但不在其中).

我现在正在以窗口模式运行游戏,但坐标基于屏幕,而不是游戏窗口(即使XNA文档告诉我它应该是另外的)

提前致谢!

这是代码:

namespace TheGame
{
   class Mousey
   {
      static public Vector2 pos;
      static private Texture2D tex;
      static public MouseState mouseState;
      static public MouseState previousState;

      //static public Mousey()
      //{
      //}

      static public void Update()
      {
         previousState = mouseState;
         mouseState = Mouse.GetState(); //Needed to find the most current mouse states.
         pos.X = mouseState.X; //Change x pos to mouseX
         pos.Y = mouseState.Y; //Change y pos to mouseY
      }

      //Drawing function to be called in the main Draw function.
      static public void LoadContent(ContentManager thecontent)
      {
         tex = thecontent.Load<Texture2D>("mousey");
      }

      static public void Draw(SpriteBatch batch) //SpriteBatch to use.
      {
         batch.Draw(tex, pos, Color.White); //Draw it using the batch.
      }

      static public bool LBP()
      {
          if (mouseState.LeftButton == ButtonState.Pressed && previousState.LeftButton ==                      ButtonState.Released)
          {
              return true; 
          }
          else 
          { 
              return false; 
          }
      }   
   }
}
Run Code Online (Sandbox Code Playgroud)

Sal*_*iti 1

你尝试过像这样更简单的事情吗?

protected override void Draw( GameTime gameTime )
{
    graphics.GraphicsDevice.Clear( Color.CornflowerBlue );

    base.Draw( gameTime );

    MouseState current_mouse = Mouse.GetState();
    Vector2 pos = new Vector2(current_mouse.X, current_mouse.Y);

    batch.Draw(tex, pos, Color.White);
}
Run Code Online (Sandbox Code Playgroud)

由于 XNA 中计时的工作方式,绘制和更新之间可能存在一些时间,这也许是感知像素偏移的原因?

而且...您确定您正确“配置”了精灵批次吗?正如文档所说,坐标是相对于游戏窗口的。

另一件事:为什么要使用静态字段?我真的不喜欢这个选择,这是一种反模式。使用类字段,而不是静态字段。

另外...我猜你正在画一个鼠标图标,对吧?考虑到 XNA 开始从指定点绘制纹理,您确定纹理形状良好,左上角点作为鼠标箭头末端吗?

我在这里找到了一个很好的例子,您可能会喜欢:http://azerdark.wordpress.com/2009/07/08/displaying-cursor-xna/

还请考虑,您可以使用 IsMouseVisible = true; 启用和禁用普通 Windows 操作系统鼠标光标;