C# XNA 鼠标位置

5
我在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; 
          }
      }   
   }
}

你的代码在我这里运行良好。我将其复制粘贴到一个新项目中。0x0位于左上角,除非你说的是1像素精度的事情。也许是spriteBatch...唯一改变的事情。 - Pierre
4个回答

3
在game.cs文件中:
//sets the windows mouse handle to client bounds handle

Mouse.WindowHandle = Window.Handle;

3
private IntPtr intPtr;

    public MouseControle(int w, int h, IntPtr intPtr)
    {
        screenwidth = w;
        screenheight = h;
        this.intPtr = intPtr;
        Mouse.WindowHandle = intPtr;
    }

这对我很有用 ;)
为了使用它,我将这个类添加到我的游戏组件中:
mouse = new MouseControle(((Game1)Game).setscreen.width, 
((Game1)Game).setscreen.height, 
((Game1)Game).Window.Handle);

希望这能对某些人有所帮助 :D

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);
}

由于 XNA 中计时方式的关系,在绘制和更新之间可能会有一些时间差,这是导致感知到的像素偏移的原因吗?

此外......您确定已经“正确配置”了 sprite 批处理吗?坐标是相对于游戏窗口的,文档中是这样说的。

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

还有......我猜你正在绘制一个鼠标图标,对吗?请注意,XNA 从指定点开始绘制纹理,您确定该纹理以左上角作为鼠标箭头结束点的形状良好吗?

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

还要考虑到您可以使用 IsMouseVisible = true; 启用或禁用标准的 Windows 操作系统鼠标光标。


我知道我做错了不止几件事情——一直都是这样。但我刚刚修复了鼠标精灵的图层控制(所以它是可见的,之前我使用的是isMouseVisible=true选项)。游戏中渲染的鼠标精灵位于游戏窗口内,并出现在我想要的位置。但它在x轴上与Windows鼠标明显偏移。感谢您的参考,我已经查阅了相当多的资料。 - Fool Whip

0

你的绘制调用没有偏移纹理。如果你的图像的“指针”部分不在纹理的0,0位置(左上角),那么定位会出现偏差。

在你的更新中添加Console.WriteLine(pos);以查看它绘制到的位置。记得在调试后删除这个,因为writeline会影响性能。

尝试使用其中一个重载的SpriteBatch.Draw()调用,它们考虑了一个“原点”,让你决定纹理的哪个点应该在位置上绘制。在下面的代码中根据你的纹理绘制方式调整Vector 2。

batch.Draw(tex, pos, null, Color.White, 0.0f, new Vector2(10, 10), SpriteEffects.None, 0.0f);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接