简体   繁体   中英

libGDX wav sound like a saw

I had problems looping a walking sound to one of my objects. My game runs on different states and I wanted the sound to loop in state "RUNNING", problem was that the wav sounded like a saw. A friend gave me a so so solution, using the following code after I created a global "long loopWalkSound = -1" in my render class:

if (myWorld.isRunning()) {
    if (loopWalkSound == -1) {
        loopWalkSound = AssetLoader.walk.loop();
    }
    drawGame(runTime);
    drawGameButtons();
    drawScore();
}

Using this code made the wav sound properly, but the moment it starts playing it doesn't stops. My intensions were for the wav to play only in the first condition's state and stop when leaving this state or not play at all when game's sound is off (which I haven't implemented for this specific wav yet but just to give you an idea). Can someone help figure it out?

Wrap it in a boolean check so you aren't restarting it on every frame. Below, change wantWalkLoopToBePlaying to whatever condition determines whether you want the sound to be playing. For example: boolean wantWalkLoopToBePlaying = state == RUNNING; .

if (!walkLoopIsPlaying && wantWalkLoopToBePlaying) {
    loopWalkSound = AssetLoader.walk.loop();
    if (loopWalkSound != -1) walkLoopIsPlaying = true;
} else if (walkLoopIsPlaying && !wantWalkLoopToBePlaying) {
    AssetLoader.walk.stop();
    walkLoopIsPlaying = false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM