public class Menu extends World{
GreenfootSound myMusic = new GreenfootSound("Stages/Opening.mp3");
GreenfootSound myMusic1 = new GreenfootSound("Stages/Stage1.mp3");
/**
* The startButton variable stores a button object.
*/
private Actor startButton;
/**
* The howToPlayButton variable stores a button object.
*/
private Actor howToPlayButton;
/**
* The historyButton variable stores a button object.
*/
private Actor historyButton;
/**
* The creditsButton variable stores a button object.
*/
private Actor creditsButton;
/**
* The Menu constructor creates a new Main Menu screen with the respective information and buttons.
*/
public Menu(){
super(600, 400, 1);
setBackground("images/World/Menu/Portada.png");
startButton = addButton("New Game",430,120,Color.WHITE);
howToPlayButton = addButton("How to play",430,200,Color.WHITE);
historyButton = addButton("History",430,280,Color.WHITE);
creditsButton = addButton("Credits",430,360,Color.WHITE);
}
/**
* The act method verifies if a button has been pressed.
* If the "New Game" button has been pressed, we initialize a new game.
* If the "How to play" button has been pressed, we display how to play information.
* If the "History" button has been pressed, we display the game history.
* If the "Credits" button has been pressed, we display the credits.
*/
public void act(){
myMusic.play();
if(Greenfoot.mouseClicked(startButton)){
myMusic.pause();
myMusic1.play();
Difficulty world = new Difficulty();
Greenfoot.setWorld(world);
}
else if(Greenfoot.mouseClicked(howToPlayButton)){
myMusic.pause();
HowToPlay world = new HowToPlay();
Greenfoot.setWorld(world);
}
else if(Greenfoot.mouseClicked(historyButton)){
myMusic.stop();
History world = new History();
Greenfoot.setWorld(world);
}
else if(Greenfoot.mouseClicked(creditsButton)){
myMusic.stop();
Credits world = new Credits();
Greenfoot.setWorld(world);
}
}
/**
* The addButton creates a new custom button on a XY coordinates.
* @return A custom button.
*/
private Actor addButton(String s, int x, int y, Color color){
Actor button = new Label(s,50,color);
addObject(button, x, y);
return button;
}
}
This way???
public void act(){
if ( ! myMusic.isPlaying() ) myMusic.play();
if(Greenfoot.mouseClicked(startButton)){
myMusic.pause();
myMusic1.play();
Difficulty world = new Difficulty();
Greenfoot.setWorld(world);
}
else if(Greenfoot.mouseClicked(howToPlayButton)){
myMusic.pause();
myMusic1.play();
HowToPlay world = new HowToPlay();
Greenfoot.setWorld(world);
}
else if(Greenfoot.mouseClicked(historyButton)){
myMusic.stop();
myMusic1.play();
History world = new History();
Greenfoot.setWorld(world);
}
else if(Greenfoot.mouseClicked(creditsButton)){
myMusic.stop();
myMusic1.play();
Credits world = new Credits();
Greenfoot.setWorld(world);
}
}