I have ghosts that spawn from the left side of the world and move right, and vice versa. However, when I go to remove them when they reach their opposite end, I can only remove from one end.
When I have it like this, a error pops up, saying
setLocation(getX()+speed, getY()); if(getX()>=getWorld().getWidth()-1) { getWorld().removeObject(this); } if(getX()>=getWorld().getWidth()+1) { getWorld().removeObject(this); }
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:681)
at greenfoot.Actor.getX(Actor.java:157)
at Ghost.act(Ghost.java:17)
at greenfoot.core.Simulation.actActor(Simulation.java:583)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:541)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
How do I fix this so I can have both types of moving ghosts get removed?