Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# XNA Mouse Position

Tags:

c#

xna

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; 
          }
      }   
   }
}
like image 401
Fool Whip Avatar asked Oct 27 '11 02:10

Fool Whip


1 Answers

In game.cs:

//sets the windows mouse handle to client bounds handle

Mouse.WindowHandle = Window.Handle;
like image 181
Jaycin Van Vuuren Avatar answered Sep 26 '22 22:09

Jaycin Van Vuuren