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

2013/12/16

How can I start and stop music on command?

GeicoCoder GeicoCoder

2013/12/16

#
I am making a Breakout game for my programming class. I'm trying to have music play during the duration of the level, but I want the theme music to stop when I die so that my death sound can be heard. I'm trying to do this with death, when I win, and when I loose all of my lives. I try to execute the following command in the same class that initially executes the theme music:
1
Greenfoot.stop(Greenfoot.playSound("Theme1.mp3"));
danpost danpost

2013/12/16

#
The 'stop' method in the Greenfoot class does not take any parameters. You cannot place anything between the parenthesis when calling 'stop' (only usage: 'Greenfoot.stop();' ). If you are using 'Greenfoot.playSound("Theme1.mp3");' to start the music, then because you do not retain a reference to the sound, you cannot stop it. It will only stop when you re-compile or leave the app. To gain more control over the music, you must create and retain a GreenfootSound object in a field so you can call methods of the GreenfootSound class on that object.
GeicoCoder GeicoCoder

2013/12/17

#
So how would I go about doing that? This is my first big project, and I don't know all of the ins and outs of Greenfoot. What is the base code for gaining more control over my sound? - Much appreciation
danpost danpost

2013/12/17

#
danpost wrote...
To gain more control over the music, you must create and retain a GreenfootSound object in a field so you can call methods of the GreenfootSound class on that object.
1
2
3
4
5
6
// add instance field to world class
GreenfootSound bgSound = new GreenfootSound("Theme1.mp3");
// in constructor (or wherever/whenever you want to start the music
bgSound.play(); // or to continuously loop through music 'bg.Sound.playLoop();'
// to stop music (from world class)
bgSound.pause(); // or 'bgSound.stop();'
A little more is involved to stop the music from another class.
GeicoCoder GeicoCoder

2013/12/17

#
Great! Cheers mate!
You need to login to post a reply.