2016-05-02 101 views
0

我想讓精靈移動到鼠標點擊(就像在大多數策略遊戲中一樣)。但是,只有當我按住鼠標左鍵的時候,我才做到這一點。當我將if子句更改爲while循環時,精靈立即進入鼠標位置。 有人可以幫我嗎?有沒有更簡單的方法通過點擊鼠標將精靈移動到特定的位置?Monogame/XNA,將精靈移動到鼠標點擊位置

最好的問候,亞歷克斯

 mCurrentMouseState = Mouse.GetState(); 

     if (mCurrentMouseState.LeftButton == ButtonState.Pressed) 
      mDestination = new Vector2(mCurrentMouseState.X, mCurrentMouseState.Y); 


     Vector2 direction = Vector2.Normalize(mDestination - mPosition); 

     mPosition += direction * (float) gameTime.ElapsedGameTime.TotalSeconds * mSpeed; 

     if (Vector2.Distance(mPosition, mDestination) < 1) 
      direction = Vector2.Zero; 

回答

0

你的代碼只每場比賽蜱移動精靈一個像素的一次。如果你想立即移動它只是使用:

if(mCurrentMouseState.LeftButton == ButtonState.Pressed) 
{ 
     mPositionBat = mCurrentMouseState; 
} 
+0

這當然比我的代碼更聰明。但我的主要問題是,即使在我釋放按鈕時,我也想讓精靈進入MouseState。 – Sancho

+0

我不太清楚你想做什麼。如果你只是想像我一樣的鼠標光標,只需使用'mPositionBat = mCurrentMouseState;'沒有任何條件。 _也作爲附註。如果這是你想要製作的第一款遊戲,那麼我建議你不要製作策略遊戲,而應該先嚐試Pong或Tetris等更簡單的方法._ – DeadlySurprise

+0

mPositionBat = mCurrentMouseState;不起作用,因爲mPositionBat是Vector2,而mCurrentMouseState是MouseState。我現在在研究,我的任務是讓一個精靈移動到你點擊鼠標的地步。當我使mPositionBat.X = mCurrentMouseState.X ;,精靈沒有緩慢移動到方向,而是立即跳到點擊鼠標的點。 – Sancho