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

2012/10/1

Error that I don't understand... Yay... Please help.

Stormtrooper Stormtrooper

2012/10/1

#
This is the error... 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 Turret.aim(Turret.java:46) at Turret.act(Turret.java:23) but what is intresting is that I used the same thing for turning towards the survivor in the zombie class, and that works...
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.
 * 
 * @author Dallin Wrathall
 */
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;
    private int pause;

    
   

    /**
     * 
     */
    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()
    {
        if (Survivor.main.getWorld() != null)
        {
         MouseInfo mouse = Greenfoot.getMouseInfo();
         if (Greenfoot.mouseMoved(null))
            setRotation(getRotationToPoint(mouse.getX(), mouse.getY()));
        move();
        checkCollision();
        checkKeys();
        reloadDelayCount++;
        mainMenu();
        if (weapon == 2)
        gunReloadTime = 17;
        if (weapon == 3)
        gunReloadTime = 14;
        if (weapon == 4)
        gunReloadTime = 11;
        if (weapon == 5)
        gunReloadTime = 9;
        upgrade();
        
    }
}
    
   
    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 (Survivor.main.getWorld() != null)
        {
        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();
        } 
        if (Greenfoot.isKeyDown("x"))
         nuke();
    }
}
    
    
    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;
    }
    public void upgrade()
    {
        if (Survivor.main.getWorld() != null)
        {
        Upgrade a = (Upgrade) getOneIntersectingObject(Upgrade.class);
        if(a != null) {
            
            upgradeWeapon(1);
            
        }
    }
}
public void nuke()
{
Bullet c = new Bullet(getMovement().copy(), getRotation());
for (int i = 0; i<= 20; i++)
            getWorld().addObject(c, getX(), getY());
             
                                                   
            c.move();
            shotsFired++;
            reloadDelayCount = 0;

}
public void mainMenu()
{
if (Survivor.main.getWorld() != null)
{
StartButton s = (StartButton) getOneIntersectingObject(StartButton.class);
TutorialButton t = (TutorialButton) getOneIntersectingObject(TutorialButton.class);
ZombieSpawner z = (ZombieSpawner) getOneIntersectingObject(ZombieSpawner.class);
MainMenu m = (MainMenu) getOneIntersectingObject(MainMenu.class);
if (s != null)
{

    MyWorld MyWorldWorld = (MyWorld) getWorld();
    MyWorldWorld.start(1);
    getWorld().removeObject(s);
    getWorld().removeObject(t);
}
if (t != null)
{

    MyWorld MyWorldWorld = (MyWorld) getWorld();
    MyWorldWorld.start(2);
    
    getWorld().removeObject(s);
    getWorld().removeObject(t);
}
if (z != null)
{
if (pause >= 0)
{
pause--;
}
else
{
getWorld().addObject (new Zombie(), 20,370);
pause = 100;
}
}
if (m != null)
{
MyWorld MyWorldWorld = (MyWorld) getWorld();
    MyWorldWorld.start(0);
    getWorld().removeObject(m);
    MyWorldWorld.wub = 1;
    MyWorldWorld.dub = 1;
}
}
}

}
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;
     private int deltaX, deltaY;
     private int deadSX, deadSY;
     public static Zombie main;

    
    public Zombie()
    {
       this(64);
       main = this;
    }
    
    /**
     * 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()
    {     
        Barrier barrier = (Barrier) getOneIntersectingObject(Barrier.class);
        if(barrier != null)
        {
           move(-10);
        }

        if (Survivor.main.getWorld() != null)
        {
        move(2);
        turnTowards(Survivor.main.getX(), (Survivor.main.getY())); 
        deadSX = Survivor.main.getX();
        deadSY = Survivor.main.getY();
    }
    else
    {
    move(1);
    turnTowards(deadSX, deadSY);
}
    
        
    }

    /**
     * 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);
        }
    }
    private void bounce(Actor a) {
        deltaY = -deltaY;
        int offset = getX()-a.getX();
        deltaX = deltaX+(offset/10);
    }
    
   
    
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Turret here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Turret extends Mover
{
 private int gunReloadTime = 25;                  
    private int reloadDelayCount;              
    private Vector acceleration;              
    public int shotsFired;
    private int pause;
    /**
     * Act - do whatever the Turret wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        aim();
    
        fire();
        reloadDelayCount++;
    }    
    private void fire() 
    {
        if (Zombie.main != null)
        {
        if(reloadDelayCount >= gunReloadTime) {
            Bullet b = new Bullet(getMovement().copy(), getRotation());
            getWorld().addObject(b, getX(), getY());
            b.move();
            shotsFired++;
            reloadDelayCount = 0;
        }
    }
    }
    private void aim()
    {
        if (Zombie.main != null)
        {
            
        turnTowards(Zombie.main.getX(), (Zombie.main.getY()));
        pause--;
    }
    else
    {
        if (Survivor.main.getWorld() != null)
        {
        turnTowards(Survivor.main.getX(), (Survivor.main.getY()));
    }
    }
    }
}
The specific code that gets pointed out is turnTowards(Zombie.main.getX(), (Zombie.main.getY()));
nooby123 nooby123

2012/10/1

#
Maybe you're using the function when either there are no zombies in the world or using it before any zombies were added into the world.
davmac davmac

2012/10/1

#
The error message pretty much tells you what the problem is: 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. In this case, the problem is that the main Zombie - "Zombie.main" in your code - has not been added to the world yet, or it has been removed. You can't "turn towards" an actor that isn't in the world!
Stormtrooper Stormtrooper

2012/10/1

#
Yes, I understand that, but shoulden't this code " if (Zombie.main != null) " make it so that it won't give me an error when the actor is not in the world?
davmac davmac

2012/10/1

#
No, references to an actor don't magically go null when you remove the actor from the world. You could use: if (Zombie.main.getWorld() != null) Or even better: if (Zombie.main != null && Zombie.main.getWorld() != null)
Stormtrooper Stormtrooper

2012/10/1

#
Thank you! I had tried your first example by using "getWorld()". However not using "getWorld()" was more succesful on how it worked when no zombie was in the world but would error when the actor was removed. But doing both made it work. Thank you davmac
You need to login to post a reply.