Hello
I want to know how to create a mute background sound button that activates and deactivates as I click on it.
I also want it to alternate between 2 images as it goes on and off.
Thank you


1 2 3 4 5 6 7 8 9 10 11 12 13 14 | private GreenfootSound backgroundMusic = new GreenfootSound( "Adventure1.mp3" ); //start the music in constructor or when "run" is pressed public void unmute() { backgroundMusic.playLoop(); } public void mute() { backgroundMusic.pause(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | private GreenfootImage[] images = { new GreenfootImage( "unmuted.png" ), new GreenfootImage( "muted.png" )}; public SoundButton() { setImage(images[ 0 ]); } public void act() { if (Greenfoot.mousePressed( this )) { if (getImage() == images[ 0 ]) { setImage(images[ 1 ]); ((WorldName) getWorld()).mute(); } else { setImage(images[ 0 ]); ((WorldName) getWorld()).unmute(); } } } |