1 回答

TA貢獻2039條經(jīng)驗 獲得超8個贊
假設(shè)你if(velocity.y != 0 || velocity.x != 0)經(jīng)常檢查,你確實會拉開很多循環(huán)。訣竅是檢查“玩家是否在移動,我上次看時他們還在嗎?” 而不僅僅是“玩家在移動”。
一種簡單的方法是設(shè)置一個布爾標志:
//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重載的文檔說明
您需要使用返回的 ID 調(diào)用 stop(long) 來停止聲音。
也許stop您正在使用的版本不起作用,或者它等待當前循環(huán)完成?
添加回答
舉報