简体   繁体   中英

Monogame/C# - Mouse position flickering

I am having some problem with mouse position in MonoGame and I can't really figure out what I am doing wrong. I want the game window to be at the position of my cursor, so I made this simple line:

protected override void Update(GameTime gameTime)
{
        Window.Position = new Point(Mouse.GetState().X, Mouse.GetState().Y);

        base.Update(gameTime);
}

But if I do this, the game window flickers between two positions. What is the problem ? Am I doing something wrong ?

Thank you!

Well it actually makes sense when you think about it. Let me make an example:

Cursor is 500,500 (in relation to Window)
Window is 300,300 (in relation to Screen)

The first time the code runs, it moves the window to 500,500. 
Meaning the cursor will now have a location of 300,300 (in relation to Window).

The second time the code runs, it will detect the mouse is now at 300,300, and thus move the Window back to 300,300.

As such it runs forever.

To accomplish what I think you want, you'd need to account for the current position of the Window.

Which means you'll have to "lock" the position of the mouse (in relation to the Window), and whenever this changes, move the Window the amount that the mouse position moved. And of course move the mouse back again

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