2014-09-19 39 views
0

如何在mouse_click事件中防止重複函數?如何在mouse_click事件XNA Gamestudio中防止重複函數?

if (Mouse.GetState().LeftButton == ButtonState.Pressed) 
{ 
      shoot_sound.Play(); 
      // it plays the sound more than once 
      // i'd like to play it just once and stop ! 
      // how to prevent key repeating 
} 
+0

可一旦你點擊它分配一個全局變量1個或什麼都值,你不添加東西,將持有的空初始值或零當第一次加載,然後..然後如果if語句檢查值和調用shoot_sound.Play()else跳出..? – MethodMan 2014-09-19 17:43:01

回答

1

您可以通過此代碼段檢查是否發生了完整的點擊。

lastMouseState = currentMouseState; 
currentMouseState = Mouse.GetState(); 
if (lastMouseState.LeftButton == ButtonState.Released && currentMouseState.LeftButton == ButtonState.Pressed) 
{ 
    // React to the click of the mouse 
    shoot_sound.Play(); 
} 
0

我不知道確切的代碼,但我想你想是這樣的..

// Create an instance of the sound - makes it easier to manage and deal with 
SoundEffectInstance sei = yourSound.CreateInstance(); 

// then when you go to play the sound make sure it isn't playing before platying it... 

if (Mouse.GetState().LeftButton == ButtonState.Pressed) 
{ 
    if (sei.State == SoundState.Playing) 
    { 
     yourSound.Play(); 
    } 
} 

對不起直接跳進一個答案問過,但我沒有足夠的聲譽只是評論,所以請告訴我,如果我對你的問題感到困惑。如果是循環播放,請嘗試使用oldMouseState和newMouseState技術來檢查您是否在播放聲音之前單擊並釋放了鼠標。真的希望這有助於。