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

2016/10/13

Question about Greenfoot sounds

JWK3986 JWK3986

2016/10/13

#
I am adding finishing touches to a lab in which we build a game called Spidertron. I am wondering if there is a command I can use between commands to play two different wav files with a pause in between so the sounds are not coming out right on top of one another. This ios what I am using to end the game and play the two sounds:
 /**
     * Method to end the game with a game over screen and sound
     */
    public void gameOverLose()
    {
        if(isTouching(Robo.class) == true){
            Greenfoot.playSound("scream.wav");
            removeTouching(Robo.class);
            getWorld().addObject(new GameOver(),300,200);
            Greenfoot.playSound("game over.wav");
            Greenfoot.stop();
        }
danpost danpost

2016/10/13

#
The way you are playing the first sound does not give you any way to determine when to start the second sound. However, if you create a GreenfootSound object and then play it, you can check to see when it has finished to play the second sound:
// replace lines 7 through 10 with
removeTouching(Robo.class);
getWorld().addObject(new GameOver(), 300, 200);
GreenfootSound sound1 = new GreenfootSound("scream.wav");
sound1.play();
while(sound1.isPlaying()) {} // wait till first sound finishes
Greenfoot.playSound("game over.wav");
A 'Greenfoot.delay(1);' line inserted after line 3 here may update the screen with the GameOver object before the sounds play.
JWK3986 JWK3986

2016/10/13

#
Thanks for the reply. Just put in the changes you specified and everything works perfectly! Thanks for the help! :)
You need to login to post a reply.