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

2015/3/20

Crashes every time I shoot the world border

Salcrainia Salcrainia

2015/3/20

#
import greenfoot.*;

public class Attack extends Variables
{
    public int speed = 5;
    
    public void act() 
    {
    }      
    
    public boolean detect(Class cls)
    {
        Actor detect = getOneObjectAtOffset(0, 0, cls);
        return detect != null;
    }
    
    public void mobHit()
    {
        if(detect(Mob.class))
        {
            kill(Mob.class);
            getWorld().removeObject(this);
        }
        else
        {
            worldBorderHit();
        }
    }   
    
    public void surfaceHit()
    {
        if((this).isTouching(Surface.class))
        {
            getWorld().removeObject(this);
        }
    }
    
    public void worldBorderHit()
    {
        Actor surface = getOneIntersectingObject(Surface.class);
        if(getX() <=1 || getX() >= getWorld().getWidth() -1)
        {
            getWorld().removeObject(this);
        }
        else if(surface != null)
        {
            getWorld().removeObject(this);
        }
    }
    
    public void kill(Class cls)
    {
        Actor kill = getOneObjectAtOffset(0, 0, cls);
        if(kill != null)
        {
            getWorld().removeObject(kill);
        }
    }
}
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 Attack.detect(Attack.java:13) at Attack.mobHit(Attack.java:19) The act occurs in a different class. Any clues as to what the issue is would be a great help :)
danpost danpost

2015/3/20

#
First, that was not the complete error message (more lines followed what you are showing above). Second, the class the act method is in should be shown.
Salcrainia Salcrainia

2015/3/20

#
import greenfoot.*;

public class Attack_Right extends Attack
{
    public void act() 
    {
        setLocation(getX() + speed, getY());
        worldBorderHit();
        mobHit();
    }    
}
import greenfoot.*;

public class Attack_Left extends Attack
{
    public void act() 
    {
        setLocation(getX() - speed, getY());
        worldBorderHit();
        mobHit();
    }    
}
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 Attack.detect(Attack.java:13) at Attack.mobHit(Attack.java:19) at Attack_Left.act(Attack_Left.java:9) 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)
danpost danpost

2015/3/20

#
Line 8 in both act methods call a method that could possibly remove the actor from the world. If a removal does occur, then the following method call cannot process properly because you cannot execute 'getOneObjectAtOffset' on an object that is not currently in a world. You must ensure that the actor is still in the world before calling any method that requires the actor be in a world if there is any chance of it not being in a world. Once removed from the world, the Actor instance method 'getWorld' will return a 'null' value. So, if it does not return a 'null' value, then it is okay to call the method:
if (getWorld() != null) mobHit();
Salcrainia Salcrainia

2015/3/20

#
That worked, thanks, it still annoys me that something that simple is a game breaker :P
danpost danpost

2015/3/20

#
Salcrainia wrote...
it still annoys me that something that simple is a game breaker :P
Well, I do not think that it is something that can be avoided that easily within the greenfoot framework; and a major part of programming involves taking into account every conceivable situation (making sure that all field and variable values are within proper limits before using them). This type of error messages usually means that this was not accomplished properly or you did not set up a field properly by not assigning a valid value to it to begin with.
You need to login to post a reply.