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

2016/3/1

IllegalStateException, actor not in world. Help.

moretinshop moretinshop

2016/3/1

#
I'm making a rogue-like maze style game for a college assignment, and the idea is that every time the player presses an arrow key and moves, the enemy actors move in a random direction. The method "move" is being sent to a "controller" class, which is inheritting from the world class, and a void is then sent from the controller to the enemy class. Upon moving, the game immediately crashes and I receive this error. Player class move method:
private void move(int velocityX, int velocityY){
        setLocation(getX() + velocityX, getY() + velocityY); //move in specified direction
        controller.move();
}
Controller class:
public class Controller extends World
{
    private static int level;
    private static int score;
    private static int lives = 3;
    private Player thePlayer;
    private Swine swineEnemy;
    private Scoreboard scoreStatus;
    public Controller()
    {    
        super(800, 600, 1);
        
        level = 1;
        
        scoreStatus = new Scoreboard();    
        thePlayer = new Player(this);
        swineEnemy = new Swine();
        
        Greenfoot.setWorld(new Level(this, thePlayer, scoreStatus, level));
    }
    public void changeWorld(){
        level++;
        Greenfoot.setWorld(new Level(this, thePlayer, scoreStatus, level));
    }
    public void setScore(int scoreIncrimant){
        score += scoreIncrimant;
    }
    public int getScore(){
        return score;
    }
    public void setLives(int livesIncrimant){
        lives += livesIncrimant;
    }
    public int getLives(){
        return lives;
    }
    public void move(){
        swineEnemy.move();
    }
}
Swine class:
public class Swine extends Mover
{
    private Controller controller;
    int destinationX;
    int destinationY;
    private boolean intersectedWall = false;
    private final int speed = 1;
    public Swine(){
    }
    
    public void setDestination(int locationX, int locationY){
        destinationX = locationX;
        destinationY = locationY;
    }
    
    public void act(){
        if(getX() != destinationX){
            if(destinationX > getX())
                setLocation(getX() + speed, getY());
            if(destinationX < getX())
                setLocation(getX() - speed, getY());
        }
        if(getY() != destinationY){
            if(destinationY > getY())
                setLocation(getX(), getY() + speed);
            if(destinationY < getY())
                setLocation(getX(), getY() - speed);
        }
    }
    
    public void move(){  
        do{
            int random = Greenfoot.getRandomNumber(4);
            if(random == 1)
                destinationY = getY() - 20;
            else if (random == 2)
                destinationX = getX() + 20;
            else if (random == 3)
                destinationY = getY() + 20;
            else if (random == 4)
                destinationX = getX() - 20;
           
            if(destinationX > getX())
                setLocation(getX() + speed, getY());
            if(destinationX < getX())
                setLocation(getX() - speed, getY());
            if(destinationY > getY())
                setLocation(getX(), getY() + speed);
            if(destinationY < getY())
                setLocation(getX(), getY() - speed);
            
            if(getOneIntersectingObject(Wall.class) != null){ //if player interesects wall
                intersectedWall = true;
                setLocation(getX(), getY());
                random = Greenfoot.getRandomNumber(4);
            }
            else
                intersectedWall = false;
                
        }while(intersectedWall);
    }
}
danpost danpost

2016/3/1

#
You should post the entire error trace. It will let you know exactly where the error is located (usually). I get the feeling that the error may be in the Level class, which you did not supply the code to.
moretinshop moretinshop

2016/3/2

#
The problem has since been solved and you're right, it was in the level class. Specifically the problem was trying to call a method from an object a line before the "addObject()" method was called, ended up being unrelated to the "move" method at all. Thanks for taking the time to reply anyway :)
You need to login to post a reply.