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

2017/4/23

java.lang.IllegalStateException: Actor not in world error

greenlemon greenlemon

2017/4/23

#
I'm making a game that essentially is a target practice game, with a target board, a gun, and bullethole objects. When trying to make my bullets move relative to the target board after being placed down, the compiler gives me an error that says the target board actor isn't in the world\\\
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class bullethole here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class bullethole extends Actor
{
    int x = 0;
    int y = 0;
    int xb = 0;
    int yb = 0;
    int xa = 0;
    int ya = 0;
    int timer = 0;
    TargetBoard target = new TargetBoard();
            /**
             * Act - do whatever the bullethole wants to do. This method is called whenever
             * the 'Act' or 'Run' button gets pressed in the environment.
             */
                public void act() 
    {
        
        if(isTouching(TargetBoard.class))
        {
            x = target.getX();
            y = target.getY();
            xa = getX();
            ya = getY();
            xb = x - xa;
            yb = y - ya;
            setLocation( getX() + xb, getY() + yb);
        }
        
        timer++;
    }
}
Super_Hippo Super_Hippo

2017/4/23

#
Line 18 creates a new (!) TargetBoard object. This is not the one which is in your world. Remove line 18 and try this instead of line 26:
Actor target = getOneIntersectingObject(TargetBoard.class);
if (target != null)
{
    //...
}
You need to login to post a reply.