2014-10-18 38 views
0

爲了從MouseState獲得單擊響應,我使用這一行。XNA:mouseState的位置是否重要?

currentMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released) 

而在該方法的末尾,我有這條線設置鼠標狀態。

oldMouseState = currentMouseState; 

我給你的問題是上面的行的位置在一個有多個循環的方法中的位置嗎?是這樣的:

  foreach (blah blah in blahs) 
     { 
      if (something is something) 
      { 
       if (currentMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released) 
       { 
        do something 
       } 
      } 
     }oldMouseState = currentMouseState; 

與此不同嗎?

  foreach (blah blah in blahs) 
     { 
      if (something is something) 
      { 
       if (currentMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released) 
       { 
        do something 
       } 
      }oldMouseState = currentMouseState; 
     } 
+1

當然你可以通過簡單地運行你的代碼來調查你的問題的結果。對我來說似乎很明顯 – MickyD 2014-10-18 05:19:40

回答

1

在第一個示例中,您的鼠標狀態更新在FOREACH循環之外。這使得在mouseState更新之前執行FOREACH循環中的所有內容。

在第二個例子中,您更新了FOREACH循環內部的mouseState,這很奇怪,但是如果是這樣,在第一個循環項之後,第二個if語句會失敗,並且您不會「執行某些操作」。