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

2012/4/3

Help! Error message

e_e13 e_e13

2012/4/3

#
Hi all, I'm busy doing a space invaders project but I've suddenly stumbled across an error message I don't understand. I've tried commenting out lines that could be the source of the problem, but to no avail. When my rocket shoots the bullet, it hits the target, and the target dissapears successfully. However, after this the game pauses and this error message comes up: 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. at greenfoot.Actor.failIfNotInWorld(Actor.java:656) at greenfoot.Actor.getX(Actor.java:157) at Bullet.atWorldEdge(Bullet.java:42) at Bullet.act(Bullet.java:32) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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. at greenfoot.Actor.failIfNotInWorld(Actor.java:656) at greenfoot.Actor.getX(Actor.java:157) at Bullet.atWorldEdge(Bullet.java:44) at Bullet.act(Bullet.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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. at greenfoot.Actor.failIfNotInWorld(Actor.java:656) at greenfoot.Actor.getX(Actor.java:157) at Bullet.atWorldEdge(Bullet.java:44) at Bullet.act(Bullet.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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. at greenfoot.Actor.failIfNotInWorld(Actor.java:656) at greenfoot.Actor.getX(Actor.java:157) at Bullet.atWorldEdge(Bullet.java:44) at Bullet.act(Bullet.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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. at greenfoot.Actor.failIfNotInWorld(Actor.java:656) at greenfoot.Actor.getX(Actor.java:157) at Bullet.atWorldEdge(Bullet.java:44) at Bullet.act(Bullet.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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. at greenfoot.Actor.failIfNotInWorld(Actor.java:656) at greenfoot.Actor.getX(Actor.java:157) at Bullet.atWorldEdge(Bullet.java:44) at Bullet.act(Bullet.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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. at greenfoot.Actor.failIfNotInWorld(Actor.java:656) at greenfoot.Actor.getX(Actor.java:157) at Bullet.atWorldEdge(Bullet.java:44) at Bullet.act(Bullet.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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. at greenfoot.Actor.failIfNotInWorld(Actor.java:656) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:860) at Bullet.act(Bullet.java:34) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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. at greenfoot.Actor.failIfNotInWorld(Actor.java:656) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:860) at Bullet.act(Bullet.java:34) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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. at greenfoot.Actor.failIfNotInWorld(Actor.java:656) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:860) at Bullet.act(Bullet.java:34) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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. at greenfoot.Actor.failIfNotInWorld(Actor.java:656) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:860) at Bullet.act(Bullet.java:34) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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. at greenfoot.Actor.failIfNotInWorld(Actor.java:656) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:860) at Bullet.act(Bullet.java:34) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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. at greenfoot.Actor.failIfNotInWorld(Actor.java:656) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:860) at Bullet.act(Bullet.java:34) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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. at greenfoot.Actor.failIfNotInWorld(Actor.java:656) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:860) at Bullet.act(Bullet.java:34) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) I think it may have to do with the bullet class, but I can't figure out what part:
public void act() 
    {
        // Add your action code here.
        
        setLocation(getX() + 30, getY() );
        
        Actor Creepinginvader;
        Creepinginvader = getOneObjectAtOffset(0,0,Invader.class);
        if (Creepinginvader!=null)
        {
            World world;
            world = getWorld();
            world.removeObject(Creepinginvader);
            world.removeObject(this);
        }
        
        Actor spasmInvader;
        spasmInvader = getOneObjectAtOffset(0,0,Invader.class);
        if (spasmInvader!=null)
        {
            World world;
            world = getWorld();
            world.removeObject(spasmInvader);
            world.removeObject(this);
        }
        
        if (this.atWorldEdge()==true)
        {
           World world;
           world = getWorld();
           world.removeObject(this);
        }
    
    }  
    // this code for removing the bullet from the edge of the screen was found here: http://www.greenfoot.org/topics/1088/15
    public boolean atWorldEdge()
    {
        if (getX() > getWorld().getWidth() - getImage().getWidth() || getY() > getWorld().getHeight() - getImage().getHeight())    
    
        {    
            return true;    
        }    
        else    
        {    
            return false;    
        }    
    }
Any help would be much appreciated :) Thanks
danpost danpost

2012/4/3

#
What is happening is that one of your checks is removing the bullet, and a following check fails because the bullet is no longer in the world (cannot use 'getOneObjectAtOffset' or 'getX' or 'getY' on an actor while it is not in the world). To fix this, you could (a) add a 'boolean removeBullet = false;' and where you 'getWorld().removeObject(this);', instead say 'removeBullet = true'; then as the last thing in act add
if (removeBullet)
{
    World world;
    world = getWorld();
    world.removeObject(this);
}
or (b) immediately after each 'world.removeObject(this)' line, insert the line 'return;', which immediately exits the method without further execution.
e_e13 e_e13

2012/4/3

#
It worked, thanks so much :)
e_e13 e_e13

2012/4/3

#
Sorry to ask again, but I seem to have stumbled upon another problem. I'm trying to make an invader that moves in a zig-zag, and although everything seems to be compiling, the game won't load when I press the big compile button on the lower-left of the screen. Here is the code for the zig-zag invader:
public void act() 
    {
        // Add your action code here.
        int v = 0;
        moveUp(v);
        moveLeft();
    }    
    
    public void moveLeft()
    {
        setLocation( getX()-1, getY());
        
    }
    
    public void moveUp(int v)
    {
        while (v<=2)
        {
            if (v%2==0)
            {
                setLocation( getX(), getY()+2);
            }
            else
            {
                setLocation( getX(), getY()-2);
            }
            if (v==2)
            {
                v=0;
            }
            v=v+1;
        }
        
    }  
I've also tried using a combination of moveUp and moveDown codes, but then the invader cancels the movement out and just moves in a straight line.
e_e13 e_e13

2012/4/3

#
I think I've found a better way to do the above, but it's still not working :/
public void act() 
    {
        // Add your action code here.
        int q=0;
        int w = 0;
        while (w<=100)
        {
            q = moveUp(q);
            moveLeft();
            w=w+1;
        }
    }    
    
    public int moveUp(int v)
    {
        v=v+1;
        if (v%2==0)
        {
            setLocation( getX(), getY()+1);
        }
        else
        {
            setLocation( getX(), getY()-1);
        }
        if (v==2)
        {
            v=0;
        }
        return v;
    }  
    
    public void moveLeft()
    {
        setLocation( getX()-1, getY());
        
    }
e_e13 e_e13

2012/4/3

#
No worries, I've finally figured it out :)
You need to login to post a reply.