当有人将鼠标放在MainMenu的按钮内时,我想播放一次声音效果。问题在这里 - MainMenu处于循环中,所以我的声音一直在重复。当有人将鼠标放在按钮内时,如何播放一次声音?
按钮:
if(mouse is in position X = <0,100> and Y = <0,100>){
drawButton( X = 0, Y = 0, X2 = 100, Y2 = 100);
}
按钮处于循环中:
while( we are in MainMenu state )
{
if(mouse is in position X = <0,100> and Y = <0,100>){
drawButton( X = 0, Y = 0, X2 = 100, Y2 = 100);
}
}
声音:
Music Sound_1 = new Music("res/Sound/Sound_1.wav");
播放声音:
Sound_1.play(1.0f, 1.0f);
代码:
while( we are in MainMenu state )
{
if(mouse is in position X = <0,100> and Y = <0,100>){
Sound_1.play(1.0f, 1.0f); <- IT MUST PLAY JUST ONCE WHENEVER MOUSE IS IN BUTTON
drawButton( X = 0, Y = 0, X2 = 100, Y2 = 100);
}
}
答案 0 :(得分:0)
您可以通过调用(使用示例声音变量Sound_1)来检查声音是否已播放:
if(mouse is in position) {
if(!sound_1.playing()) {
sound_1.play(1.0f, 1.0f);
}
drawButton(x,y,x2,y2);
}
使用声音/音乐对象具有光滑的.playing()方法,如果声音当前正在播放,则返回布尔值true,否则返回false。希望这会有所帮助:)
答案 1 :(得分:0)
在调用循环之前,添加一个布尔值来跟踪游戏是否应播放声音:
boolean playSound = false;
然后,当您调用循环时,请执行以下操作:
loop() {
playSound = true;
if (/**Mouse is on the button*/) {playSound = true;}
else {playSound = false}
//If playSound is true, then play the sound
if (playSound) {sound.play(1.0f, 1.0f);}
else
return;
}
这应该有用!
答案 2 :(得分:0)
您需要设置一个布尔值来确定我们是否已播放声音。
boolean soundHasPlayed = false;
while( we are in MainMenu state )
{
if(mouse is in position X = <0,100> and Y = <0,100>){
if (!soundHasPlayed) // only do this if we haven't played the sound yet
{
Sound_1.play(1.0f, 1.0f);
drawButton( X = 0, Y = 0, X2 = 100, Y2 = 100);
soundHasPlayed = true; // don't play it again until we leave the button
}
}
else
{
// We left the button, so we can play it again if we ever enter the button again.
// (Omit this if you really only want it to play once, ever, even if they mouse over the button again.)
soundHasPlayed = false;
}
}