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

2012/9/17

Actor not in world error?

Stormtrooper299 Stormtrooper299

2012/9/17

#
I get an error of the acotr not being in the world, but the actor is in the world... "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 Zombie.act(Zombie.java:51)" Here is the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;

/**
 * 
 * 
 * @author Dallin Wrathall
 */
public class MyWorld extends World
{
public int level;
private Counter theCounter;
private Survivor theSurvivor;
    public MyWorld() 
    {
        super(600,400, 1);
        GreenfootImage background = getBackground();
        background.setColor(Color.WHITE);
        background.fill();
        theCounter = new Counter();
        addObject(new Survivor(), 300, 200);
        theSurvivor = new Survivor();

        addObject(theCounter, 300, 20);

        
       
        }

  
    public void act()
    {
        // level 0 is actually level 1
        if (level == 0)
        {
        if (Greenfoot.getRandomNumber(1000) < 5) {
            addObject(new Zombie(),0,0) ;
        }
        if (Greenfoot.getRandomNumber(1000) < 5) {
            addObject(new Zombie(), 600, 0);
        }
        if (Greenfoot.getRandomNumber(1000) < 5) {
            addObject(new Zombie(), 600,400);
        }
        if (Greenfoot.getRandomNumber(1000) < 5) {
            addObject(new Zombie(), 0, 400);
        }
    }
        
        //level 1 is actually level 2
        if (level == 1)
        {
        if (Greenfoot.getRandomNumber(300) < 5) {
            addObject(new Zombie(),0,0) ;
        }
        if (Greenfoot.getRandomNumber(300) < 5) {
            addObject(new Zombie(), 600, 0);
        }
        if (Greenfoot.getRandomNumber(300) < 5) {
            addObject(new Zombie(), 600,400);
        }
        if (Greenfoot.getRandomNumber(300) < 5) {
            addObject(new Zombie(), 0, 400);
        }
    }
    //level 2 is actually level 3
    if (level == 2)
        {
        if (Greenfoot.getRandomNumber(100) < 5) {
            addObject(new Zombie(),0,0) ;
        }
        if (Greenfoot.getRandomNumber(100) < 5) {
            addObject(new Zombie(), 600, 0);
        }
        if (Greenfoot.getRandomNumber(100) < 5) {
            addObject(new Zombie(), 600,400);
        }
        if (Greenfoot.getRandomNumber(100) < 5) {
            addObject(new Zombie(), 0, 400);
        }
    }
    }
     public Counter getCounter()
    {
        return theCounter;
    }
    public Survivor getSurvivor()
    {
        return theSurvivor;
    }
    public void levelSwitch(int amount)
    {
        level += amount;
        
    }
  
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A survivor of the zombie apocolypse, control using a,w,s,d.
 * The gun is fired by hitting the 'space' key.
 * 
 *  */
public class Survivor extends Mover
{
    private int gunReloadTime;                  
    private int reloadDelayCount;              
    private Vector acceleration;              
    public int shotsFired;                    
    public static Survivor main;
    public static boolean survivorAlive;
    public int weapon;

    
   

    /**
     * 
     */
    public Survivor()
    {
        main = this;
        gunReloadTime = 20;
        reloadDelayCount = 0;
        shotsFired = 0;
        survivorAlive = false;
    }

    /**
     * Do what a Survivor's gotta do. (Which is:shooting when the rig keys are pressed.)
     */
    public void act()
    {
        survivorAlive = true;
         MouseInfo mouse = Greenfoot.getMouseInfo();
         if (Greenfoot.mouseMoved(null))
            setRotation(getRotationToPoint(mouse.getX(), mouse.getY()));
        move();
        checkCollision();
        checkKeys();
        reloadDelayCount++;
        if (weapon == 2)
        gunReloadTime = 15;
        
    }
    
   
    public int getShotsFired()
    {
        return shotsFired;
    }
    
   
    public void setGunReloadTime(int reloadTime)
    {
        gunReloadTime = reloadTime;
    }
    
    
    private void checkCollision() 
    {
       Zombie a = (Zombie) getOneIntersectingObject(Zombie.class);
        if(a != null) {
            getWorld().addObject(new Explosion(), getX(), getY());
            survivorAlive = false;
            getWorld().removeObject(this);
        }
    }
    
    /**
     * Check whether there are any key pressed and react to them.
     */
    private void checkKeys() 
    {
        if(Greenfoot.isKeyDown("w"))
        {
            setLocation(getX(),(getY()-2));
        }
        
        if(Greenfoot.isKeyDown("a")) {
           setLocation(getX() - 2, getY());
        }        
        if(Greenfoot.isKeyDown("d")) {
            setLocation(getX() + 2, getY());
        }
        if (Greenfoot.isKeyDown("s"))
        {
            setLocation(getX(),(getY()+2));
        }
        if(Greenfoot.isKeyDown("space")) {
            fire();
        }        
    }
    
    
    private void ignite(boolean boosterOn) 
    {
        if(boosterOn) {

            acceleration.setDirection(getRotation());
            increaseSpeed(acceleration);
        }
        else {
                 
        }
    }
    
    /**
     * Fire a bullet if the gun is ready.
     */
    private void fire() 
    {
        if(reloadDelayCount >= gunReloadTime) {
            Bullet b = new Bullet(getMovement().copy(), getRotation());
            getWorld().addObject(b, getX(), getY());
            b.move();
            shotsFired++;
            reloadDelayCount = 0;
        }
    }
    public int getRotationToPoint(int x, int y)
    {
        int angle = 1;
        if (getX() > x && getY() > y)
        {
            int opp = getY() - y;
            int adj = getX() - x;
            angle = (int)(180 + Math.toDegrees(Math.atan(opp/adj)));
        }
        else if (getX() > x && getY() < y)
        {
            int opp = y - getY();
            int adj = getX() - x;
            angle = (int)(180 - Math.toDegrees(Math.atan(opp/adj)));
        }
        else if (getX() < x && getY() > y)
        {
            int opp = getY() - y;
            int adj = x - getX();
            angle = (int)(0 - Math.toDegrees(Math.atan(opp/adj)));
        }
        else if (getX() < x && getY() < y)
        {
            int opp = y - getY();
            int adj = x - getX();
            angle = (int)(360 + Math.toDegrees(Math.atan(opp/adj)));
        }
        return angle;
    }
    public void upgradeWeapon(int amount)
    {
        weapon += amount;
    }
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A brain eating infected dude.
 * 
 * 
 * @author Dallin Wrathall
 */
public class Zombie extends Mover
{
    /** Size of this Zombie */
    private int size;

    /** When the Zombie's life reaches 0 the Zombie will die */
    private int stability;
    public int killed;
    

    
    public Zombie()
    {
       this(64);
       
    }
    
    /**
     * Create an Zombie with a given size and default speed.
     */
    public Zombie(int size)
    {
        size = Greenfoot.getRandomNumber(200);
    }
    
    /**
     * Create an Zombie with a given size size and speed.
     */
    private Zombie(int size, Vector speed)
    {
        super(speed);
        setSize(size);
    }
    
    /**
     * Let the Zombie act. That is: go to Survivor.
     */
    public void act()
    {     
        move(2);

        
        turnTowards(Survivor.main.getX(), (Survivor.main.getY())); 
    
        
    }

    /**
     * Set the size of this Zombie. 
     */
    public void setSize(int size) 
    {
        stability = size;
        this.size = size;
        GreenfootImage image = getImage();
        image.scale(size, size);
    }

    /**
     * Return the current stability of this Zombie. (If it goes down to 
     * zero, it breaks up.)
     */
    public int getStability() 
    {
        return stability;
    }
    
    /**
     * Hit this Zombie dealing the given amount of damage.
     */
    public void hit(int damage) {
        stability = stability - damage;
        if(stability <= 0) 
        {

           getWorld().removeObject(this);
        }
    }
    
   
    
    
}
The weird thing is that it worked before...
erdelf erdelf

2012/9/17

#
didn't looked much at it (no time), but be sure that a survivor is there during calling of this method.
davmac davmac

2012/9/17

#
Line 51, the zombie always tries to turn towards the survivor, even if the survivor has been removed from the world. That's what's causing the problem. You can't get the location of something that's not in the world!
Stormtrooper299 Stormtrooper299

2012/9/18

#
The actor is in the world so that's not the problem. But I figured out that if I delete line 13, 22, and 87-90 on MyWorld, it works. But I need that for something else, any suggestions?
danpost danpost

2012/9/18

#
In line 51 of Zombie class, if you put a condition on the turntowards (that is, find out if the Survivor.main object is in the world before you turntowards it), you should be able to rid yourself of the error.
if (Survivor.main.getWorld() != null) turnTowards(Survivor.main.getX(), Survivor.main.getY());
However you probably do not want the Zombie to move at all if the Survivor.main is NOT in the world; so, you may want this as your Zombie act method instead
public void act()
{
    if (Survivor.main.getWorld() != null)
    {
        move(2);
        turnTowards(Survivor.main.getX(), Survivor.main.getY());
    }
}
davmac davmac

2012/9/18

#
The problem is that the survivor isn't in the world (even if the zombie is). Like I already said, you can't turn towards something that's not in the world.
danpost danpost

2012/9/18

#
You could put an 'else' block on this to have the Zombie objects 'wander' (move around randomly) when the Survivor.main object is not in the world.
You need to login to post a reply.