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

2015/2/5

Error pops up every time my Object is removed.

GabrahamCAPS GabrahamCAPS

2015/2/5

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Seal here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Seal extends Predator
{
    
    /**
     * Act - do whatever the Seal wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
     if (isOffScreen())
     {
          getWorld().removeObject(this);
     }
     
     if (foundPlayer())
     {
          eatPlayer();
     }
     setLocation(getX() - 10, getY());
}

public boolean foundPlayer()
    {
        Actor player = getOneObjectAtOffset(0, 0, Player.class);
        if(player != null) {
            return true;
        }
        else {
            return false;
        }
    }
    
    public boolean isOffScreen()
{
     if (getX() < 0 - getImage().getWidth()/2 || 
getX() > getWorld().getWidth() + getImage().getWidth()/2 ||
getY() < 0 - getImage().getHeight()/2 ||
getY() > getWorld().getHeight() + getImage().getHeight()/2)
     {
          return true;
     }
     return false;
}

public void eatPlayer()
{
    Actor player = getOneObjectAtOffset(0, 0, Player.class);
    if(player != null) {
        getWorld().removeObject(player); 
    }
}
}
I would also love it if I could have an explanation of understanding. Thanks!
GabrahamCAPS GabrahamCAPS

2015/2/5

#
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.getOneObjectAtOffset(Actor.java:867) at Seal.foundPlayer(Seal.java:32) at Seal.act(Seal.java:23) 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)
GabrahamCAPS GabrahamCAPS

2015/2/5

#
the Code in Bold is where the error seems to occur. If you need more code for the error, let me know. Thanks!
danpost danpost

2015/2/5

#
If you follow the act method step by step, you will get this: Block 1 (lines 18 through 21): removes the actor from the world if off sceeen; Block 2 (lines 23 though 26): removes any center-intersecting player; Block 3 (line 27): moves actor left; If the actor is removed from the world by block 1, then neither block 2 nor block 3 can execute properly. Both 2 and 3 require that the actor be in the world. When you execute 'getWorld().removeObject(this)', the hidden 'world' field from the Actor class for that actor is set to 'null'. No in-world operations can be executed on that actor if the field is set to 'null'; this includes, but is not limited to, collision checking and movement commands. You can either force an exit from the method after removing the actor from the world by inserting a 'return' statement or check for off-screen last (move block 1 to the end -- after blocks 2 and 3).
GabrahamCAPS GabrahamCAPS

2015/2/5

#
danpost wrote...
If you follow the act method step by step, you will get this: Block 1 (lines 18 through 21): removes the actor from the world if off sceeen; Block 2 (lines 23 though 26): removes any center-intersecting player; Block 3 (line 27): moves actor left; If the actor is removed from the world by block 1, then neither block 2 nor block 3 can execute properly. Both 2 and 3 require that the actor be in the world. When you execute 'getWorld().removeObject(this)', the hidden 'world' field from the Actor class for that actor is set to 'null'. No in-world operations can be executed on that actor if the field is set to 'null'; this includes, but is not limited to, collision checking and movement commands.
So what should I do?
GabrahamCAPS GabrahamCAPS

2015/2/5

#
danpost wrote...
If you follow the act method step by step, you will get this: Block 1 (lines 18 through 21): removes the actor from the world if off sceeen; Block 2 (lines 23 though 26): removes any center-intersecting player; Block 3 (line 27): moves actor left; If the actor is removed from the world by block 1, then neither block 2 nor block 3 can execute properly. Both 2 and 3 require that the actor be in the world. When you execute 'getWorld().removeObject(this)', the hidden 'world' field from the Actor class for that actor is set to 'null'. No in-world operations can be executed on that actor if the field is set to 'null'; this includes, but is not limited to, collision checking and movement commands. You can either force an exit from the method after removing the actor from the world by inserting a 'return' statement or check for off-screen last (move block 1 to the end -- after blocks 2 and 3).
Oh, I see now. You make complete sense. Thank you so much. You are greatly appreciated for replying so quickly and explaining it so well. Thanks again.
You need to login to post a reply.