This site requires JavaScript, please enable it in your browser!
Greenfoot back
mattjames
mattjames wrote ...

2013/8/16

Removing an actor

1
2
danpost danpost

2013/8/16

#
Or just modify what you have to this:
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);
}
As an alternative, you could create a new World sub-class for the menu:
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
    }
}
danpost danpost

2013/8/16

#
If 'playbutton' is the name of a class, then:
getWorld().removeObjects(getWorld().getObjects(playButton.class));
would remove the instance of it from any sub-class of Actor. EDIT: my alternative above may not be what you want (without seeing your world class, it is hard to determine exactly what you are doing).
mattjames mattjames

2013/8/16

#
Danpost that last getWorld().removeObjects(getWorld().getObjects(playButton.class)); was perfect thank you sooo much needed that all day!! and I am trying to keep everything in one world.
You need to login to post a reply.
1
2