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

2013/12/22

isTouching() error!!!

fastslash8 fastslash8

2013/12/22

#
Hello! I am new to greenfoot, and this error always pops up for me. Here is the error code for this case 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:663) at greenfoot.Actor.isTouching(Actor.java:925) at Player.onFloor(Player.java:22) at CaveMkr.act(CaveMkr.java:103) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) Here is the Player code
public class Player extends Actor
{
    boolean onfloor = true;
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        getWorld().setPaintOrder(Player.class, Tile.class,CaveMkr.class);
    }    
    public boolean onFloor()
    {
        return isTouching(Tile.class);
    }
}
And here is the CaveMkr code throwing an error
if(Greenfoot.isKeyDown("left")){
                    wall.setLocation(wall.getX()+1, wall.getY());
                    if(player.onFloor() == false){
                        wall.setLocation(wall.getX()-1, wall.getY());
                    }
                }
Thanks for helping!
danpost danpost

2013/12/22

#
Change line 3 of the CaveMkr 'if' block to:
if (player.getWorld() != null && !player.onFloor()) {
This will ensure that 'player' is in the world before calling the 'onFloor' method on it.
fastslash8 fastslash8

2013/12/22

#
Whoops, got a little ahead of myself! Actually, the player continues to move even if not touching the path, which is what that code was supposed to detect. Sorry to be a bother, but can anyone help? Thanks!
You need to login to post a reply.