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

2013/6/7

stoping the music

MBX5 MBX5

2013/6/7

#
i added music into my world class and when the world is running, the music plays but i want the music to stop when my player dies. how can i do this?
Zamoht Zamoht

2013/6/7

#
I guess you are using the GreenfootSound class to play the sound, and if this is true you should be able to stop the music by using the GreenfootSound method pause() or stop().
MBX5 MBX5

2013/6/7

#
ive tired that but it is not stoping
danpost danpost

2013/6/7

#
A sample GreenfootSound object:
GreenfootSound sound = new GreenfootSound("soundFileName.wav");
sound.play();
// later
sound.pause();
// or
sound.stop();
MBX5 MBX5

2013/6/7

#
nope that doesnt work either. heres the coding i have for it. i have added in the stop code but its not working
private GreenfootSound music = new GreenfootSound("Music.mp3");  
  
    public void started()  
    {  
        music.play();
        if(getObjects(Ship.class).isEmpty())
        {
            music.stop()
        }       
    } 
danpost danpost

2013/6/7

#
It is not working because the 'started' method is only called once (when the scenario goes from a stopped state to a running state). It would be alright to start the music there; but, to stop the music you would have to constantly check to see if the Ship object was not in the world. That can only be done in the 'act' method or a method it calls.
MBX5 MBX5

2013/6/7

#
so what do you suggest?
danpost danpost

2013/6/7

#
Move the 'if' condition/block to the act method of that class (if you do not yet have one, create one)
You need to login to post a reply.