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

2012/10/17

Background music

Mirza Mirza

2012/10/17

#
Can any one tell mi what is wrong with this code? I am traying to play music buy clicking on the object ans stop it if I click again. public GreenfootSound music = new GreenfootSound("killer.mp3"); /** * Act - do whatever the Dugme wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { play(); stop(); } public void play() { if(Greenfoot.mouseClicked(this)) { music.playLoop(); } } public void stop() { if (music.isPlaying() && Greenfoot.mouseClicked(this)) { music.stop(); } }
danpost danpost

2012/10/17

#
In 'stop' you have two conditions, but in 'play' you only have one. I am not quite sure if the music will continuously play or appear to not play at all, but either way, the code will not do what you want without the other condition in the first 'if' statement. You only want the music to play (start) if it is 'not playing' currently. Therefore, you need to add the condition '!music.isPlaying' to the code.
Mirza Mirza

2012/10/17

#
if I chang it like this nothing happens. public void play() { if( !music.isPlaying() && Greenfoot.mouseClicked(this)) { music.playLoop(); } } public void stop() { if (music.isPlaying() && Greenfoot.mouseClicked(this)) { music.stop(); } }
erdelf erdelf

2012/10/17

#
of course nothing happens. you tell the object that the music starts and in the same act circle the music has to stop
SPower SPower

2012/10/17

#
Better is to put it all in one method:
private void handleMusic()
{
     if( !music.isPlaying() && Greenfoot.mouseClicked(this)) {
          music.playLoop();
          return;
     } else if (music.isPlaying() && Greenfoot.mouseClicked(this)) {
         music.stop();
     }
}
Mirza Mirza

2012/10/17

#
I am not sure whar do I have to change and how?
Mirza Mirza

2012/10/17

#
Thank you!!
You need to login to post a reply.