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

2017/8/18

removing bullet when at edge

mole2003 mole2003

2017/8/18

#
for some reason (wich I can't fathom) whenever the bullet reaches the edge it pops up an error box: 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:711) at greenfoot.Actor.getX(Actor.java:164) at base3.act(base3.java:25) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) 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:711) at greenfoot.Actor.isTouching(Actor.java:972) at bullet.act(bullet.java:23) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) 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:711) at greenfoot.Actor.isTouching(Actor.java:972) at bullet.act(bullet.java:23) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) 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:711) at greenfoot.Actor.isTouching(Actor.java:972) at bullet.act(bullet.java:23) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) 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:711) at greenfoot.Actor.isTouching(Actor.java:972) at bullet.act(bullet.java:23) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) my code for the entire bulletclass is:
public class bullet extends Actor
{
    /**
     * Act - do whatever the bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setRotation(270);
        move(5);
        if (isAtEdge())
        {
           getWorld().removeObject(this); 
        }
        if (isTouching(alienbullet.class))
        {
            getWorld().removeObject(this);
        }
    }
    
}
Super_Hippo Super_Hippo

2017/8/18

#
If it is at the edge, it will be removed. After that, it tries to check if it is touching an alienbullet but this fails because it is not in the world anymore. Change the 'if' in line 15 to 'else if', so it is not executed anymore when it was already removed from the world.
mole2003 mole2003

2017/8/19

#
thanks worked perfectly.
You need to login to post a reply.