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

2013/8/16

Removing an actor

1
2
mattjames mattjames

2013/8/16

#
Sorry the API couldn't explain it to me how do you remove an actor from the world??
SPower SPower

2013/8/16

#
in the world itself:
removeObject(>the object to be removed<);
OR from the object which needs to be removed:
getWorld().removeObject(this);
and it does explain it. Just look the method 'removeObject' in the World class up.
Gevater_Tod4711 Gevater_Tod4711

2013/8/16

#
You have to use the method removeObject(Actor). Therefore you need the reference to the object you want to remove. E.g. when you want to remove an object that touches another object you can do it like this:
public void act() {
    Actor actor = getOneIntersectingObject(Actor.class);
    if (actor != null) {
        getWorld().removeObject(actor);
        //if you want to remove an object from an actor class you have to use getWorld().removeObject;
    }
}
mattjames mattjames

2013/8/16

#
Okay so how do I write it from another actor but not for the getOneIntersecting menthod?
danpost danpost

2013/8/16

#
If you already have a reference, 'reference', to the actor you want to remove, just use
getWorld().removeObject(reference);
If you do not have a reference and you want to remove all instances of a class, 'ClassName', that are in the world:
getWorld().removeObjects(getWorld().getObjects(ClassName));
mattjames mattjames

2013/8/16

#
Excellent thanks
mattjames mattjames

2013/8/16

#
Tried using that code (bottom one) but it came up with getObjects(java.lang.Class) in greenfoot.World cannot be applied t (greenfoot.Actor) Any ideas as to what I am doing wrong? and what did you mean by a reference?
danpost danpost

2013/8/16

#
Declaring 'Actor actor = null;' sets up a reference (a field that can contain an object of given type). Once 'actor' is assign an Actor object to that field, whether by 'actor = getOneIntersectingObject(ClassName.class);' or some other way, you can use the field name 'actor' to reference that Actor object. The 'getObjects(Class)' method needs a class name followed by '.class', or 'null' for all Actor classes in your project, as its argument, not an Actor object.
mattjames mattjames

2013/8/16

#
Okay thanks is there any code that will remove all actors in the world at once?
SPower SPower

2013/8/16

#
getWorld().removeObjects(getWorld().getObjects(null));
because the getObjects method returns all objects when you enter 'null' as an argument/parameter
mattjames mattjames

2013/8/16

#
Hi that worked great but then gave me a problem -->
public void act() 
    {
       
        if (Greenfoot.mouseClicked(this))
        {
            
                            getWorld().removeObjects(getWorld().getObjects(null));
            addobjects();

    }
    }    
    public void addobjects(){
    getWorld().setBackground(new GreenfootImage("modification background.fw.png"));  //this line then comes up with a nullpointerexception 
        getWorld().addObject(new skisbutton(), 3 , 2);
        getWorld().addObject(new tracksbutton(), 5 , 2);
        getWorld().addObject(new wheelsbutton(), 7 , 2);
        getWorld().addObject(new largebaybutton(), 7 , 3);
        getWorld().addObject(new mediumbaybutton(), 5 , 3);
        getWorld().addObject(new smallbaybutton(), 3 , 3);
        getWorld().addObject(new backbutton(), 8 , 7);
    }
any ideas??
SPower SPower

2013/8/16

#
Well, when you remove all objects, it means that the actor (which you are calling the method from), also gets removed. This means that it's world doesn't exist anymore. So getWorld(), which you use in line 13, returns null and you can't use it anymore.
SPower SPower

2013/8/16

#
I advice you move this code to your world, doing this:
// in your actor class:
if (Greenfoot.mouseClicked(this)) {
    ((YourWorldName) getWorld()).removeAllObjects();
}
in your world class:
public void removeAllObjects()
{
    removeObjects(getObjects(null));
    addObject(new skisbutton(), 3 , 2);
    // add all the other objects you want to add, but remove the 'getWorld().' part at the beginning from it.
}
And just a little advice, I see your classes' names start with a lower case, which won't cause a crash, but doesn't make an optimal class name. You better start your name with an Uppercase instead, so that you won't confuse others :)
mattjames mattjames

2013/8/16

#
ahhh okay thanks a lot
mattjames mattjames

2013/8/16

#
I'm not going to lie, I still dont get this remove actor stuff. Can someone please write the code that will fit in this please I will be really appreciative
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class modifybutton here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class modifybutton extends buttons
{
    public int traction=0;
    public int baysize=0;
    /**
     * Act - do whatever the modifybutton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       
        if (Greenfoot.mouseClicked(this))
        {
            
        //code to get rid of actor called playbutton from world called world


    
       
 
        getWorld().setBackground(new GreenfootImage("modification background.fw.png"));  //this line then comes up with a nullpointerexception 
                    getWorld().addObject(new skisbutton(), 3 , 2);
        getWorld().addObject(new tracksbutton(), 5 , 2);
        getWorld().addObject(new wheelsbutton(), 7 , 2);
        getWorld().addObject(new largebaybutton(), 7 , 3);
        getWorld().addObject(new mediumbaybutton(), 5 , 3);
        getWorld().addObject(new smallbaybutton(), 3 , 3);
        getWorld().addObject(new backbutton(), 8 , 7);
    }
}
I need to get rid of an actor called playbutton. It is in Actors and then in a subclass called buttons. World is called world. Pls help need to get this done ASAP. Thanks
There are more replies on the next page.
1
2