I have a game that has a Menu world and a MyWorld world that plays the game. I am having a problem getting my music to stop when the world changes from MyWorld to Menu by clicking a button called back. Sound also has a button that turns on and off the sound that works just fine.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Sound here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Sound extends Actor
{
GreenfootSound backgroundMusic;
boolean firstTurn = true;
GreenfootImage image1 = new GreenfootImage("soundon.png");
GreenfootImage image2 = new GreenfootImage("sound.png");
public Sound(String musicName)
{
backgroundMusic = new GreenfootSound(musicName);
image1.scale(image1.getWidth() - 470, image1.getHeight() - 455);
setImage(image1);
}
/**
* Act - do whatever the Sound wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
Button();
over();
}
public void over()
{
if(Greenfoot.mouseClicked(Back.class)) {
backgroundMusic.stop();
}
}
public void Button() {
if (firstTurn) {
backgroundMusic.play();
firstTurn = false;
}
if (Greenfoot.mouseClicked(this)) {
if (backgroundMusic.isPlaying()) {
backgroundMusic.pause();
image2.scale(image1.getWidth(), image1.getHeight());
setImage(image2);
}
else {
backgroundMusic.playLoop();
setImage(image1);
}
}
}
}