简体   繁体   English

Monogame / C#-鼠标位置闪烁

[英]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. 我在MonoGame中的鼠标位置遇到了一些问题,我无法真正弄清楚自己在做什么错。 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. 要实现我认为想要的功能,您需要考虑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. 这意味着您必须“锁定”鼠标的位置(相对于Window),并且每当更改此位置时,就将Window移动到鼠标位置移动的量。 And of course move the mouse back again 当然还要再次将鼠标移回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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