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

2013/10/14

Help with sound

gdwd3 gdwd3

2013/10/14

#
I am trying to make a sound that will start then stop when the game ends. My code below:
        GreenfootSound sound = new    GreenfootSound("music.wav");
        GreenfootSound sound2 = new GreenfootSound("death.wav");
        
        if(playno == 1)
        {
            sound.play();
            playno++;
        }
        
        if(canSee(Player.class))
        {
            eat(Player.class);
            sound.stop();
            Greenfoot.stop();
        }
My problem is that when the game ends the music doesn't stop. Any help will be much appreciated.
Gevater_Tod4711 Gevater_Tod4711

2013/10/14

#
This code probably is from an enemy class. If so the problem is that there are many sounds playing at the same time and you only stop one of them. To fix this problem you could try to change line 1 to: private static GreenfootSound sound = new GreenfootSound("music.wav"); The modifier static causes that there is only one sound for all your enemy objects and so it should stop the sound.
danpost danpost

2013/10/14

#
@Gevater_Tod4711, you are correct in that making the GreenfootSound objects 'static' will ensure that the 'start' and 'stop' refer to the same object. There is another possibility, however, if you only have one object of this class created. The 'sound.stop()' command is stopping the same GreenfootSound object that 'sound.play()' started; however, unless the value of 'playno' is changed again or the actor created from the class the given code is within is removed from the world, the first 'if' block will re-start the sound, making it appear not to stop at all.
gdwd3 gdwd3

2013/10/14

#
I did what you said Gevater_Tod4711 and it worked so thank for the help :D
danpost danpost

2013/10/14

#
I had edited my post to qualify what I was trying to say.
You need to login to post a reply.