C# XNA 鼠标位置

发布于 2024-12-12 06:33:08 字数 1388 浏览 2 评论 0原文

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

I am having some issues with my mouse coordinates in XNA - the 0x0 is arbitrarily near (but not in) the top left corner of my screen.

I am running the game in a windowed mode right now, but the coordinates are based off the screen, not the game window (even though the XNA documentation tells me it should be otherwise)

Thanks in advance!

Here's the code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

源来凯始玺欢你 2024-12-19 06:33:08

在游戏.cs中:

//sets the windows mouse handle to client bounds handle

Mouse.WindowHandle = Window.Handle;

In game.cs:

//sets the windows mouse handle to client bounds handle

Mouse.WindowHandle = Window.Handle;
暮倦 2024-12-19 06:33:08
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

private IntPtr intPtr;

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

This works for me ;)

To use this, I add this to my game-component using that class:

mouse = new MouseControle(((Game1)Game).setscreen.width, 
((Game1)Game).setscreen.height, 
((Game1)Game).Window.Handle);

Hope this helps sombody :D

单调的奢华 2024-12-19 06:33:08

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

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 中计时的工作方式,绘制和更新之间可能存在一些时间,这也许是感知像素偏移的原因?

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

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

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

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

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

Did you try something simpler like this?

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

There may be some time between draw and update, due to the way timing works in XNA, maybe is this the cause of the perceived pixel offset?

And... are you sure you "configured" your sprite batch correctly? Coordinates are relative to game window, so the documentation say.

Another thing: Why are you using static fields? I really don't like this choice, an anti-pattern. Use class fields, not static fields.

Also... i guess you are drawing a mouse icon, right? consider that XNA start to draw the texture from the specified point, are you sure the texture is well shaped with the top-left point as your mouse arrow end?

I found a nice example here you may like: http://azerdark.wordpress.com/2009/07/08/displaying-cursor-xna/

Consider also that you can enable and disable the normal windows OS mouse cursor with IsMouseVisible = true;

生生漫 2024-12-19 06:33:08

您的绘制调用根本不会抵消纹理。如果图像的“指针”部分不在纹理的 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);

Your draw call is not offsetting the texture at all. If the "pointer" part of your image isn't in the 0,0 position (top left) of your Texture, the positioning will seem off.

Add a Console.WriteLine(pos); to your update to see the position it is drawing to. Remember to remove this after your debugging because writeline will kill your performance.

Try one of the overloaded SpriteBatch.Draw() calls which factor in an "origin" which lets you decide which point of the texture should be drawn at the position. In the following code tweak the Vector 2 based upon how your texture is drawn.

batch.Draw(tex, pos, null, Color.White, 0.0f, new Vector2(10, 10), SpriteEffects.None, 0.0f);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文