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

2012/2/25

java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.

TopInPut TopInPut

2012/2/25

#
if(atWorldEdge()) {
getWorld().removeObject(this);
}
If i use that code, an attempt comes. -.- java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
TopInPut TopInPut

2012/2/25

#
How I can remove the object without getting that message ?
danpost danpost

2012/2/25

#
Would need to see more code to solve this problem. Although, the message is telling you that the actor is NOT in the world, regardless. Might have to see the whole class.
TopInPut TopInPut

2012/2/25

#

import greenfoot.*;  

/**
 * Write a description of class Munition here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Munition extends Mover
{
    /**
     * Act - do whatever the Munition wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(25.0); 
        if(atWorldEdge()) {
        getWorld().removeObject(this);            
        }
        if(onEnemys()) {
            eat(Enemys.class);
            getWorld().removeObject(this);
        }
    }    
}

In the act is the problem
danpost danpost

2012/2/26

#
To avoid this error message, it is always best to have a return statement after removing the object (if more code follows).
public void act()
{
    move(25.0);
    if(atWorldEdge()) {
        getWorld().removeObject(this);
        return; //this return is neccessary so the following statements do not execute
    }
    if(onEnemys()) {
        eat(Enemys.class);
        getWorld().removeObject(this);
        return; //this return is not neccessary as is last statement in act()
    }
}
TopInPut TopInPut

2012/2/26

#
Thx, that's it. ;)
Joep Joep

2012/3/1

#
couldn't you have used an if-else statement? I'm new with greenfoot, but I think that will do the trick aswell.
danpost danpost

2012/3/1

#
Yes, only because there is no other code in the method. However, if more code was added later on, the if-else if statement would not prevent that code from executing, and would probably cause a run-time error (trying to do something to an object that is no longer in the world). Use of the 'return;' statement, especially after removing the object from the world, is more of a fail-safe.
You need to login to post a reply.