如何确保指定声音只有 1 个实例在循环以及如何阻止它循环?
How do I make sure only 1 instance of a specified Sound is looping and how do I stop it from looping?
我目前正在开发一款游戏,在播放器更新方法中,我希望足迹 Sound
在玩家行走时开始循环,并在玩家停止行走时停止循环。但是,我无法弄清楚如何确保只有 Sound
的 1 个实例在循环。
澄清一下:我说的是 gdx.audio.Sound
class。这是我的代码目前的样子:
//Sets the footstep sound. i.e. it changes to a grass soundfile when you walk on grass.
String footstepsFilePath = gameMap.getTileSoundFilePath(rect);
setFootsteps(Gdx.audio.newSound(Gdx.files.internal(footstepsFilePath)));
//velocity is the speed at which the player is going in the x or y direction.
if(velocity.y != 0 || velocity.x != 0) footsteps.loop();
if(velocity.y == 0 && velocity.x == 0) footsteps.stop();
结果:一旦玩家开始移动,大量的脚步声实例就会开始循环。当播放器停止移动时,所有播放器都会继续循环。第一部分是出于显而易见的原因,但我无法弄清楚如何确保只有一个实例在循环。但是对于第二部分,我不确定为什么不是所有的脚步声实例都停止循环,因为这是 stop()
上的文档所说的:
Stops playing all instances of this sound.
假设你经常检查if(velocity.y != 0 || velocity.x != 0)
,你确实会启动很多循环。诀窍是检查 "is the player moving, and were they still last time I looked?" 而不仅仅是 "is the player moving".
一个简单的方法是设置一个布尔标志:
//Sets the footstep sound. i.e. it changes to a grass soundfile when you walk on grass.
String footstepsFilePath = gameMap.getTileSoundFilePath(rect);
setFootsteps(Gdx.audio.newSound(Gdx.files.internal(footstepsFilePath)));
boolean isMoving = false;
//velocity is the speed at which the player is going in the x or y direction.
if((velocity.y != 0 || velocity.x != 0) && !isMoving) {
isMoving = true;
footsteps.loop();
}
if((velocity.y == 0 && velocity.x == 0) && isMoving) {
footsteps.stop();
isMoving = false;
}
我不完全确定为什么 stop
对您的情况不起作用。但是,其他两个 loop
重载的文档指出
You need to stop the sound via a call to stop(long) using the returned id.
也许您正在使用的 stop
版本不工作,或者它正在等待当前循环完成?
我目前正在开发一款游戏,在播放器更新方法中,我希望足迹 Sound
在玩家行走时开始循环,并在玩家停止行走时停止循环。但是,我无法弄清楚如何确保只有 Sound
的 1 个实例在循环。
澄清一下:我说的是 gdx.audio.Sound
class。这是我的代码目前的样子:
//Sets the footstep sound. i.e. it changes to a grass soundfile when you walk on grass.
String footstepsFilePath = gameMap.getTileSoundFilePath(rect);
setFootsteps(Gdx.audio.newSound(Gdx.files.internal(footstepsFilePath)));
//velocity is the speed at which the player is going in the x or y direction.
if(velocity.y != 0 || velocity.x != 0) footsteps.loop();
if(velocity.y == 0 && velocity.x == 0) footsteps.stop();
结果:一旦玩家开始移动,大量的脚步声实例就会开始循环。当播放器停止移动时,所有播放器都会继续循环。第一部分是出于显而易见的原因,但我无法弄清楚如何确保只有一个实例在循环。但是对于第二部分,我不确定为什么不是所有的脚步声实例都停止循环,因为这是 stop()
上的文档所说的:
Stops playing all instances of this sound.
假设你经常检查if(velocity.y != 0 || velocity.x != 0)
,你确实会启动很多循环。诀窍是检查 "is the player moving, and were they still last time I looked?" 而不仅仅是 "is the player moving".
一个简单的方法是设置一个布尔标志:
//Sets the footstep sound. i.e. it changes to a grass soundfile when you walk on grass.
String footstepsFilePath = gameMap.getTileSoundFilePath(rect);
setFootsteps(Gdx.audio.newSound(Gdx.files.internal(footstepsFilePath)));
boolean isMoving = false;
//velocity is the speed at which the player is going in the x or y direction.
if((velocity.y != 0 || velocity.x != 0) && !isMoving) {
isMoving = true;
footsteps.loop();
}
if((velocity.y == 0 && velocity.x == 0) && isMoving) {
footsteps.stop();
isMoving = false;
}
我不完全确定为什么 stop
对您的情况不起作用。但是,其他两个 loop
重载的文档指出
You need to stop the sound via a call to stop(long) using the returned id.
也许您正在使用的 stop
版本不工作,或者它正在等待当前循环完成?