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

2016/3/18

Actor not in world error

scanlan scanlan

2016/3/18

#
A game where you play as a seal to collect anchors and boats but have to avoid the fish. The program runs fun but when i hit a fish i get an error that pops up everytime. pls help 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:695) at greenfoot.Actor.isTouching(Actor.java:956) at Seal.act(Seal.java:59) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
/**
 * 
 */
public class Seal extends Actor
{
 public int fishScore = 0;
 public int time;
    /**
     * 
     */
    public void act()
    {
       
        if (Greenfoot.isKeyDown("s"))
       {
        setRotation(90);
        move(2);
       }
        if (Greenfoot.isKeyDown("w"))
       {
        setRotation(270);
        move(2);
       }
       if (Greenfoot.isKeyDown("a"))
       {
        setRotation(180);
        move(2);
       }
       if (Greenfoot.isKeyDown("d"))
       {
        setRotation(0);
        move(2);
       }
       if (isTouching(Boat.class))
       {
         Ocean ocean = (Ocean)getWorld();
         ocean.addScore(20);
         removeTouching(Boat.class);
       }
       if (isTouching(Anchor.class))
       {
         Ocean ocean = (Ocean)getWorld();
         ocean.addScore(10);
         removeTouching(Anchor.class);
       }
       if (isTouching(Fish1.class))
       {
         Greenfoot.stop(); 
         getWorld().showText("Timer:" + (time - time), 450,30);
         getWorld().showText("Game Over", 300,200);
         getWorld().removeObjects(getWorld().getObjects(null));    
       }
        if (isTouching(Fish2.class))
       {
         Greenfoot.stop(); 
         getWorld().showText("Timer:" + (time - time), 450,30);
         getWorld().showText("Game Over", 300,200);
         getWorld().removeObjects(getWorld().getObjects(null));    
       }
        if (isTouching(Fish3.class))
       {
         Greenfoot.stop(); 
         getWorld().showText("Timer:" + (time - time), 450,30);
         getWorld().showText("Game Over", 300,200);
         getWorld().removeObjects(getWorld().getObjects(null));      
       }
}
}
danpost danpost

2016/3/18

#
You have three places where you remove all objects from the world -- specifically at lines 51, 58 and 65. If either lines 51 or 58 executes, 'isTouching' in the following 'if' condition cannot execute properly as the Seal object is also removed from the world. Because all code blocks for those three 'if's are exactly the same (lines 48 thru 51, lines 55 thru 58 and lines 62 thru 65), you can combine the conditions into one 'if' condition:
if (isTouching(Fish1.class) || isTouching(Fish2.class) || isTouching(Fish3.class))
Alternatively, if you have an intermediate Fish class between Actor and the three fish types, you could just ask:
if (isTouching(Fish.class))
Either way, you would only have one condition, or set of conditions, and the last line executed would be the removal of the actors, which avoids any related IllegalStateException or NullPointerException errors.
You need to login to post a reply.