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

2013/6/12

How do I make the music in my game play only when I'm pressing a button?

Chocomint1213 Chocomint1213

2013/6/12

#
So far I only have this. if( Greenfoot.isKeyDown("a") ) I don't know what else to do. Help
Zamoht Zamoht

2013/6/12

#
Okay in the start of what ever class you want to put the code in you should declare a variable: public GreenfootSound sound = new GreenfootSound("filename.format"); And then after your if( Greenfoot.isKeyDown("a") ) you should use: if (!sound.isPlaying()) //checks that the sound isn't already playing { sound.play(); //plays the sound }
danpost danpost

2013/6/12

#
If you want the music to start when you press the key and stop when you release it:
if (!sound.isPlaying() && Greenfoot.isKeyDown("a")) sound.play();
if (sound.isPlaying() && !Greenfoot.isKeyDown("a")) sound.pause();
You need to login to post a reply.