Or just modify what you have to this:
As an alternative, you could create a new World sub-class for the menu:
public void act()
{
if (Greenfoot.mouseClicked(this)) addObjects();
}
public void addobjects()
{
World world = getWorld();
world.removeObjects(world.getObjects(null));
world.setBackground(new GreenfootImage("modification background.fw.png"));
world.addObject(new skisbutton(), 3 , 2);
world.addObject(new tracksbutton(), 5 , 2);
world.addObject(new wheelsbutton(), 7 , 2);
world.addObject(new largebaybutton(), 7 , 3);
world.addObject(new mediumbaybutton(), 5 , 3);
world.addObject(new smallbaybutton(), 3 , 3);
world.addObject(new backbutton(), 8 , 7);
}public void act()
{
if (Greenfoot.mouseClicked(this)) Greenfoot.setWorld(new Menu(getWorld());
}
// with the new world Menu subclass:
import greenfoot.*;
public class Menu extends World
{
private World main;
public Menu(World world)
{
super(>width<, >height<, >cellsize<);
main = world;
setBackground(new GreenfootImage("modification background.fw.png"));
addObject(new skisbutton(), 3 , 2);
addObject(new tracksbutton(), 5 , 2);
addObject(new wheelsbutton(), 7 , 2);
addObject(new largebaybutton(), 7 , 3);
addObject(new mediumbaybutton(), 5 , 3);
addObject(new smallbaybutton(), 3 , 3);
addObject(new backbutton(), 8 , 7);
}
public void act()
{
// code for button click detection here
// examples
if (>back button clicked<) back();
if (>wheels button clicked<) applyWheels();
}
// button action methods here
// examples
private void back()
{
Greenfoot.setWorld(main);
}
private void applyWheels()
{
Player player = (Player) main.getObjects(Player.class).get(0);
player.applyWheels(); // calls method in Player class to put wheels on
}
}
