C# XNA Mouse Position

后端 未结 4 591
醉酒成梦
醉酒成梦 2021-01-13 06:05

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

4条回答
  •  春和景丽
    2021-01-13 06:51

    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;

提交回复
热议问题