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

2014/12/5

Getting an Error Message When I Kill an Enemy

Kickero Kickero

2014/12/5

#
I've include a picture of the error and my code for the enemy... sorry i know its alot of code but didn't have another way... The error reads as follows 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:681) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:885) at Porcubutt.move(Porcubutt.java:62) at Porcubutt.act(Porcubutt.java:40) at greenfoot.core.Simulation.actActor(Simulation.java:583) at greenfoot.core.Simulation.runOneLoop(Simulation.java:541) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
public class Porcubutt extends Characters
{
    private GreenfootImage imageR;
    private GreenfootImage imageL;
    private int Health = 1;
    private boolean IsAttacking = false;
    private int stillAttacking = 100;
    private int spriteHeight = getImage().getHeight();
    private int spriteWidth = getImage().getWidth();
    private int lookForEdge = (int)spriteHeight/2;
    private int lookForGroundDistance = (int)spriteWidth/2;
    private int speed = 2;
    private int direction = 1; //-1=left 1=right
    public int kills = 0;
    private int SBHealth = 90;
   

    public Porcubutt()
    {
        imageR = new GreenfootImage ("PorcuButt_R.png");
        imageL = new GreenfootImage ("PorcuButt_L.png");
    }

    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        SirBAlert();
        checkAttack();
        move();
    }    

    public void checkAttack()
    {
        if(Greenfoot.isKeyDown("space") && stillAttacking >= 50 )
        {
            IsAttacking = true;
            stillAttacking = stillAttacking -1;
        }
        else
        {
            IsAttacking =false;
            if (IsAttacking = false && stillAttacking < 100 )
            {
                stillAttacking = stillAttacking +1;
            }
        }
    }

    public void move()
    {
        Actor ground = getOneObjectAtOffset(lookForEdge, lookForGroundDistance, Platforms.class);
        if(ground == null)
        {
            speed *= -1; // Reverses direction
            lookForEdge *= -1; // Looks for a negative number
        }
        else
        {
            setLocation (getX() + speed, getY());
        }
        if (speed == 2)
        {
            direction = 1;
            setImage(imageR);
        }
        else
        {
            direction = -1;
            setImage(imageL);
        }

    }

    public void SirBAlert()
    {
        if (canSee(SirB.class))
        {
            checkSirB();
        }
    }

    public void checkSirB()
    {

        if (IsAttacking == true )
        {
            Greenfoot.playSound("Squeak.wav");
            Health --;
        }
        else
        {
            SBHealth =SBHealth-15;
            Greenfoot.playSound("Hurt.wav");

        } 
        if (Health <= 0)
        {
            getWorld().removeObject(this);
        }
        if (SBHealth <= 0)
        {
            Greenfoot.stop();
        }
    }

   

    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }

    public void setSBHealth(int SBHealth)
    {
        this.SBHealth=SBHealth;
    }

    public void setIsAttacking(boolean IsAttacking)
    {
        this.IsAttacking=IsAttacking;
    }

    public void setStillAttacking(int stillAttacking)
    {
        this.stillAttacking=stillAttacking;
    }

    public void setKills(int kills)
    {
        this.kills=kills;
    }

}
danpost danpost

2014/12/5

#
The error occurs when the health of the actor gets to zero. When this happens, your act method is calling 'SirBAlert' which calls 'checkSirB'. Your health values goes to zero or below and the porcubutt is removed from the world. Execution then returns back to the act method, when 'checkAttack' is called. This method can execute fully as written without any problems because nothing in the method requires the actor be in the world (it only deals with fields and keystrokes). Then, finally execution back at the act method calls the method 'move'. The first line in that method (line 54 above -- line 62 in your class code) requires the actor be in the world for 'getOneObjectAtOffset'. The method cannot run because it has no world (or coordinates) to get an offset from -- hence the error. Somewhere between calling 'SirBAlert' and 'move' in the act method (preferably immediately after calling 'SirBAlert'), you must break out of the method if the actor was removed from the world, so that the rest of the method does not execute when it is removed. A simple 'if' checking what 'getWorld' returns would be sufficient. If it returns 'null' then 'return' out of the method.
You need to login to post a reply.