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

2014/3/18

Actor's location Error

MontclairState MontclairState

2014/3/18

#
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. I tried to only check for location if the actor is in the world but I feel I must of used the wrong coding. This is the code for the actor being removed.
public class Person extends Actor
{
    public static Person main; 
    private int i_MouseX,i_MouseY; 
    public Person() { 
        main = this; 
    }
    public void act() 
    {
        MouseInfo MI_ = Greenfoot.getMouseInfo();  
        if(MI_!=null)  
    {  
        i_MouseX=MI_.getX();  
        i_MouseY=MI_.getY();
       
    } 
    setLocation(i_MouseX, i_MouseY);
    
    Actor dead = getOneIntersectingObject(Animal.class);
        if (dead != null) {
                getWorld().removeObject(this);
                return;
        }
    }    
}
This is the code of the actor following the actor that will be removed if it catches it.
public class Animal extends Actor
{
   private int i_PersonX,i_PersonY;
    
    public void act() 
    {
         if(Person.main != null)  
    {  
        i_PersonX=Person.main.getX();  
        i_PersonY=Person.main.getY();
       
    } 
        turnTowards(i_PersonX, i_PersonY); 
        move(1);
    }    
}
danpost danpost

2014/3/18

#
You need to make sure, in the Animal class, that the person is in the world before trying to get its location coordinates. Change the first line in the 'act' method of your Animal class to:
if (Person.main != null && Person.main.getWorld() != null)
You may want to move the last two statements in that 'act' method to within the 'if' block. Otherwise, your Animal will go to the last place the person was and go spastic (unless you want that).
MontclairState MontclairState

2014/3/18

#
At first, I tried saying getWorld().Person.main != null but it said that was incorrect so I was so confused. Thank you danpost for clearing that up for me.
You need to login to post a reply.