如何防止在事件XNA游戏工作室mouse_click重复功能

本文关键字:click 功能 mouse 工作室 何防止 事件 XNA 游戏 | 更新日期: 2023-09-27 18:22:35

如何防止事件mouse_click重复函数?

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 
 }

如何防止在事件XNA游戏工作室mouse_click重复功能

您可以通过此代码段检查是否发生了完整的点击。

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

我不确切知道代码,但我认为你想要这样的东西。

// 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();
    }
}

很抱歉在提问之前直接回答,但我没有足够的声誉来发表评论,所以如果我对您的问题感到困惑,请告诉我。如果这是您遇到问题的循环,请尝试使用旧的鼠标状态和新鼠标状态技术来检查您的鼠标是否已被单击并释放,然后再播放声音。真的希望这有帮助。