I am getting this error when my game ends and I can't figure out how to fix it.
I'm not sure what other code you need to see to help, but here is the act method for my crab.
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.getOneIntersectingObject(Actor.java:930)
at Crab.heartCollisions(Crab.java:163)
at Crab.act(Crab.java:41)
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)
This happens when my crab object in the game runs into another object or loses all of it's health. I have code in there that I thought would stop the game, so I don't know why it's still looking for an actor at this point.
This is part of the code where the game ends if the crab touches a pelican object. All objects are removed and it shows my game over image, and also returns the error above.
healthbar.loseHealth(); touchingPelican = true; World world = getWorld(); world.removeObjects(world.getObjects(null)); world.setBackground("gameover.jpg"); Greenfoot.stop();
public void act() { checkKeypress(); lookForWorm(); lobsterCollisions(); pelicanCollisions(); heartCollisions(); scoreKeeper(); }