Or just modify what you have to this:
As an alternative, you could create a new World sub-class for the menu:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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 ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 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 } } |