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

2015/2/4

Help Stopping Music

shooterbooth shooterbooth

2015/2/4

#
I have been trying to stop my background music when my game ends. It starts playing when you compile and run the program but it keeps playing after the game ends and the gameover sound plays. This is what I have in one of my actor classes right now because I couldn't figure it out in the world class either: GreenfootSound sound = new GreenfootSound("GameOfThrones.mp3"); public void endGame() { if (crabEaten == 1) { sound.stop(); Greenfoot.playSound("gameover.mp3"); Greenfoot.stop(); } }
danpost danpost

2015/2/4

#
How you create and/or start the music will determine whether or how to stop it.
shooterbooth shooterbooth

2015/2/5

#
How would you recommend doing this? I have tried several different ways of doing this and have failed every time.
danpost danpost

2015/2/5

#
danpost wrote...
How you create and/or start the music will determine whether or how to stop it.
The post quoted here was an attempt to have you show what you used, code-wise, to create and start the music to begin with. Without knowledge of this, it would be almost impossible to help in the matter. However, I can say that you should not declare a new field for the sound in the actor class. That just creates a NEW sound object (which is not the one that is playing). Stopping it does not stop the one playing.
shooterbooth shooterbooth

2015/2/5

#
public void stopped() { backgroundMusic.playLoop(); GreenfootSound gameover = new GreenfootSound("gameover.mp3"); if (gameover.isPlaying()) { backgroundMusic.stop(); } } I created this method in my world class. backgroundMusic is in the first line as a world instance variable and I want the backgroundMusic to play until the other music starts.
davmac davmac

2015/2/5

#
First, please use code tags when you post code: Now, in the code you posted you are creating a new sound and then immediately checking if it is playing:
 GreenfootSound gameover = new GreenfootSound("gameover.mp3");
 if (gameover.isPlaying())
The condition can never be true. You only just created the sound object, so there is no way it can be playing. You need to use the same sound object that you initially started playing, and check if that object is still playing. You cannot create a new sound object and check if that one is playing. As a first step you need to change:
 Greenfoot.playSound("gameover.mp3");
Into code which plays a GreenfootSound object which you maintain a reference to (so that you can check later whether it is still playing).
You need to login to post a reply.