我正试图在gameState更改时播放声音
例如我在主菜单中,单击“说明”并播放声音。我点击后退按钮再次播放相同的声音。
我在执行此操作时遇到了很多麻烦!
case GameState.MainMenu:
if (PlayButton.isClicked == true) CurrentGameState = GameState.Levels;
menuSoundPlayOnce = true;
PlayButton.Update(mouse);
if (instructionsButton.isClicked == true) CurrentGameState = GameState.Instructions;
instructionsButton.Update(mouse);
if (quitButton.isClicked == true)
Exit();
quitButton.Update(mouse);
break;
case GameState.Instructions:
if (backButton.isClicked == true) CurrentGameState = GameState.MainMenu;
backButton.Update(mouse);
if (!menuToMenuSoundPlayOnce)
{
menuToMenuSound.Play();
}
menuToMenuSoundPlayOnce = true;
break;
我只使用代码
播放一次声音if (!menuToMenuSoundPlayOnce)
{
menuToMenuSound.Play();
}
menuToMenuSoundPlayOnce = true;
但它播放一次,当然menuToMenuSoundPlaceOnce设置为false。 如果我改变它以保持虚假,它只是一遍又一遍地播放声音。
我似乎无法在网上找到这个。
感谢您的帮助
答案 0 :(得分:0)
尝试这样的事情:
//Get a instance of the sound effect -- do this once only
SoundEffectInstance menuSoundInstance = menuToMenuSound.CreateInstance();
//Play the sound during update, if the sound is not already playing
if (menuSoundInstance.State == SoundState.Stopped)
{
menuToMenuSound.Play();
}