Hi guys,
First of all I'll mention that I'm still fairly new to GreenFoot.
I've progressed pretty far in creating a maze game, but I stumbled upon a pretty odd and especially annoying error.
In the maze there are a few bolts flying around as well as a wallmaster walking around. Whenever touching either I remove all objects from the world and show a "you lost the game" screen. Now, for the wallmaster this all works perfectly, but whenever I touch a bolt I get the following error:
For my Player class:
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.getOneObjectAtOffset(Actor.java:885)
at Player.canSee(Player.java:109)
at Player.deathByWallmaster(Player.java:144)
at Player.act(Player.java:42)
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)
My code is as follows (I shrunk parts down to what would matter)
For the Bolt class:
public class Bolt extends Actor { private int movespeed = 1; /** * Act - do whatever the Bolt wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(); } public void move() { //moves bolt from bottom-right to top-right if(getX() >= 75) { setLocation(getX() + movespeed, getY()); } //moves wallmaster from top-right to top-left // Actor Bolt; if(getX() == 165) { World world = getWorld(); world.removeObjects(getWorld().getObjects(Bolt.class)); world.addObject(new Bolt(), 75, 375); world.addObject(new Bolt(), 75, 405); world.addObject(new Bolt(), 75, 435); } }
public class Player extends Actor { public void act() { //Calling all methods deathByBolts(); } public boolean canSee(Class clss) { return getOneObjectAtOffset(0, 0, clss) != null; } public void itemPick(Class cls) { Actor actor = getOneObjectAtOffset(0, 0, cls); if(actor != null) { getWorld().removeObject(actor); } } public void deathByBolts() { if(canSee(Bolt.class)) { World world = getWorld(); world.removeObjects(getWorld().getObjects(null)); world.addObject(new Lose(), world.getWidth()/2, world.getHeight()/2); } }