Hello Everyone..
I have problem with my game project, every the game started and play in between 20 - 30 second, the game always error and have problem like this :
NOTICE
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 Laser.act(Laser.java:33)
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 Laser.act(Laser.java:33)
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 Underline Red
public void act()
{
if(!toRemove){
setLocation(getX()+vx,getY());
Actor actor=getOneIntersectingObject(Alien.class);
if(actor!=null){
World myWorld = getWorld();
((Alien)actor).Hancur();
Latar latar = (Latar)myWorld;
Counter counter = latar.getCounter();
counter.addScore();
}
if(getX()>getWorld().getWidth()+200)toRemove=true;
}else{
getWorld().removeObject(this);
}
if(isTouching(Alien.class))
{
getWorld().removeObject(this);
}
}
}
Anyone can help me ? Please :)
Reason for the error: The actor is removed from the world and then you are calling a method which requires the actor to be in the world (in this case 'isTouching').
When the actor is removed from the world, the current act method will be finished.
I don't really know why you need this code at all. You earlier check for the intersection with the Alien. If you want it to be removed, then remove it there. The 'toRemove' variable also seems to be useless.