Matthew Goulart
Matthew Goulart

Reputation: 3065

XNA mouse position is largely offset from ACTUAL position

I have here some simple code for getting the mouse position and drawing it:

    class Pointer
{
    MouseState currPointerState;
    Vector2 currPointerPos;


    public Pointer()
    {
    }

    public void Update()
    {
        currPointerState = Mouse.GetState();
        currPointerPos.X = currPointerState.X;
        currPointerPos.Y = currPointerState.Y;
    }

    public void Draw(SpriteBatch spriteBatch, Texture2D pointerTexture)
    {
        spriteBatch.Draw(pointerTexture, currPointerPos, Color.White);
    }

}

Then in my main game file:

        protected override void Update(GameTime gameTime)
    {
        pointer.Update();
        base.Update(gameTime);
    }

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

        spriteBatch.Begin();

        pointer.Draw(spriteBatch, pointerTexture);
        menuManager.Draw(spriteBatch, menu_bar);

        spriteBatch.End();
        base.Draw(gameTime);
    }

The game runs in a window for now, however the mouse is about 500 pixels too far right and 100 pixels to far below the actual mouse position.

This occurred after I added the following code: (a menu drawing class)

    enum MenuState
{
    mainMenu,
    playing,
    options
};

class MenuManager : Game1
{
    MenuState menuState = MenuState.mainMenu;
    Vector2 button1 = Vector2.Zero;


    public void Draw(SpriteBatch spriteBatch, Texture2D menuBar)
    {
        switch (menuState)
        {
            case MenuState.mainMenu:
                spriteBatch.Draw(menuBar, button1, Color.White);
                    break;



        }
    }
}

}

Any idea why the mouse position might have been changed?

Upvotes: 1

Views: 157

Answers (1)

Matthew Goulart
Matthew Goulart

Reputation: 3065

So i figured it out.

In case anyone is having trouble, the solution was to remove the : Game1 reference as bass class. The game class was being created more than once, hence the window coordinates were being stacked.

Upvotes: 4

Related Questions