2015-10-04 121 views
0

我在MonoGame中遇到了一些與鼠標位置有關的問題,我無法弄清楚我做錯了什麼。我想遊戲窗口是在我的光標的位置,所以我做了這個簡單的一行:Monogame/C# - 鼠標位置閃爍

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

     base.Update(gameTime); 
} 

但如果我這樣做,遊戲窗口在兩個位置之間閃爍。問題是什麼 ?難道我做錯了什麼 ?

謝謝!

+0

出於好奇。你爲什麼想這樣做? – craftworkgames

回答

0

嗯,當你考慮它時,它確實有意義。讓我舉個例子:

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. 

因此它永遠運行。

爲了完成我認爲你想要的,你需要考慮窗口的當前位置。

這意味着您必須「鎖定」鼠標的位置(相對於窗口),並且每當此更改時,將窗口移動鼠標位置移動的量。當然,再次移動鼠標