简体   繁体   中英

C# XNA Mouse Position

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

In game.cs:

//sets the windows mouse handle to client bounds handle

Mouse.WindowHandle = Window.Handle;
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

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM