This site requires JavaScript, please enable it in your browser!
Greenfoot back
lilnasX
lilnasX wrote ...

2023/1/19

Need to execute sound.stop() once a sound has played through once

lilnasX lilnasX

2023/1/19

#
This is my code, I want the class to play through the wasted sound once, and then once it has finished playing play the gameOverBGM sound in a loop. However, the if statements condition is never met and the gameOverBGM line is never reached. I can't just input wasted.stop() after wasted.play otherwise the sound will not play through once and it will immediately play my gameOverBGM. need some advice, thanks.
public class GameOverWorld extends World
{
    static GreenfootSound wasted = new GreenfootSound("Wasted.mp3");
    static GreenfootSound gameOverBGM = new GreenfootSound("EndGameMusic.mp3");
    public GameOverWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1200, 675, 1); 
        prepare();
        wasted.play();
        if(!wasted.isPlaying()){
            gameOverBGM.playLoop();
        }
        }
    void prepare(){
        addObject(new BackToMenu(), 315, 450);
        addObject(new MenuIndicator(), 250, 200);
    }
    public void act(){
    
            if (getObjects(GameOver.class).isEmpty()) showGameOver();
            return;
        
    }
    private void showGameOver()
    {
        addObject(new GameOver(), getWidth() / 2, getHeight() / 2);
    }
    public static GreenfootSound getMusic(){
        return gameOverBGM;
    }
}
danpost danpost

2023/1/19

#
lilnasX wrote...
I want the class to play through the wasted sound once, and then once it has finished playing play the gameOverBGM sound in a loop. However, the if statements condition is never met and the gameOverBGM line is never reached. I can't just input wasted.stop() after wasted.play otherwise the sound will not play through once and it will immediately play my gameOverBGM. need some advice, thanks. << Code Omitted >>
You cannot track the progress of the sound from the constructor of the class. Use the act method for this control. Also, line 22 is a bit redundant. The end of the method marks an automatic return. You can remove that line.
You need to login to post a reply.