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

2019/4/18

Actor not in world error

NoobFected NoobFected

2019/4/18

#
So I am trying to make code for laser collision, where whenever the laser is shot and collides with a body part of a centipede, the centipede and the laser will disappear. When I run my code, this happens, but then it follow up with an error saying: 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:714) How do I solve this issue? The issue is apparently on line 46 for the centipede class Here is my code for the laser:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Laser here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Laser extends Actor
{
    /**
     * Act - do whatever the Laser wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public final int speed;
    
    public Laser(){
        speed = 10;
    }
    
    public void act() 
    {
        setLocation(getX(), getY() - speed);
    }   
}
and the code for the centipede:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * Write a description of class Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Centipede extends Actor
{
    /**
     * Act - do whatever the Ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    boolean bounceWall;
    int width; 
    int height; 
    int dx;
    int dy;
    int counter;
    int bounceCounter;
    
   public Centipede(){
        GreenfootImage image = getImage();
        image.scale(25, 25);
        
        bounceWall = false;
        width = image.getWidth();
        height = image.getHeight();
        dx = width / 5;
        dy = 0;
        counter = 0;
        bounceCounter = 0;
        setImage(image);
    }
    
    public void act() {   
        if(counter != 0){
            wallCollision();
        }
        counter++;
        mushroomCollision();
        laserCollision();
        setLocation(getX() + dx, getY() + dy);
        dy = 0;
        bounceWall = false;
    }    
    
Super_Hippo Super_Hippo

2019/4/18

#
In one of the methods which were before (wallCollision, mushroomCollision or laserCollision), the Laser was removed from the world. The act method still finishes a last time. The setLocation method requires the Actor to be in a world which it isn't. So you get the error.
NoobFected NoobFected

2019/4/18

#
Is there like a way to fix this though? I still want to keep the setLocation method.
danpost danpost

2019/4/18

#
NoobFected wrote...
Is there like a way to fix this though? I still want to keep the setLocation method.
Insert the following immediately before the setLocation line:
if (getWorld() == null) return;
NoobFected NoobFected

2019/4/19

#
danpost wrote...
NoobFected wrote...
Is there like a way to fix this though? I still want to keep the setLocation method.
Insert the following immediately before the setLocation line:
if (getWorld() == null) return;
Thanks, it works perfectly now.
You need to login to post a reply.