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

2019/10/29

Illegal State Exception: actor trying to get location of player when it has been removed from the world

comfortablyNumb comfortablyNumb

2019/10/29

#
Hello been trying to get this done for a while now. I've written a method in my actor subclass to check if it isTouching the player and once it is it runs some basic code and then removes itself. but if the actor gos out of play without touching the player it gives me the error msg
java.lang.IllegalStateException: Actor not in world. An attemp 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.
ive tried a few things to stop to statement from running if the actor isAtEdge() or getY() > 0 but these still give the same errors heres the full class
public class Questions extends fallingActors
{
    /**
     * Act - do whatever the Questions wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    protected static boolean answerCorrect;
    public void act() 
    {
        answerCorrect = false;
        setLocation();
        checkRemove(this);
        collideWithPlayer();
    }    
    
    private void collideWithPlayer()
    {
        if(isAtEdge() == false)
        {
            Actor player = getOneIntersectingObject(player.class);
            //send question
            if(player != null)
            {
                Greenfoot.ask("hello world");
                World w = getWorld();
                GameWorld.score += 1;  
                //after asking question remove object
                w.removeObject(this);   
            }
        }
    }
}
Hundmat Hundmat

2019/10/29

#
Try writing this code in your act method
if (getWorld() == null) return;
Hundmat Hundmat

2019/10/29

#
I had the same problem when I tried to switch the world after an actor got removed. This could work I'm not sure though
danpost danpost

2019/10/29

#
Hundmat wrote...
I had the same problem when I tried to switch the world after an actor got removed. This could work I'm not sure though
Yes. But it must be placed properly. In this case after line 12
comfortablyNumb comfortablyNumb

2019/10/30

#
so using above the method would end the method early?
danpost danpost

2019/10/30

#
comfortablyNumb wrote...
so using above the method would end the method early?
Yes. The execution of the method will be immediately broken out of if the actor is not in any world at the time that line executes.
comfortablyNumb comfortablyNumb

2019/11/4

#
epico its working now :)
You need to login to post a reply.