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

2017/3/23

Error Hitting Enemy

Jillis Jillis

2017/3/23

#
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:711) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:913) at Bullet.hitBoss(Bullet.java:26) at Bullet.act(Bullet.java:37) I receive this error when shooting an enemy with a bullet. I thought that the line saying if boss!= null would stop this? Bullet class:
public class Bullet extends Actor
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void hitEnemy()
    {
        EnemyNormal enemynormal = (EnemyNormal) getOneObjectAtOffset(0, 0, EnemyNormal.class);
        if(enemynormal != null)
        {
            enemynormal.setHealth(-20);
            getWorld().removeObject(this);
        }
    }
    public void hitBoss()
    {
        Boss boss = (Boss) getOneObjectAtOffset(0, 0, Boss.class);
        if(boss != null)
        {
            boss.setHealthBoss(-20);
            getWorld().removeObject(this);
        }
    }
    public void act() 
    {
        move(10);
        hitEnemy();
        hitBoss();
    }
}
danpost danpost

2017/3/23

#
It is not the boss that is the problem -- it is the actor looking for the boss, the bullet itself. When it hits an enemy, it is removed from the world; but your code continues on and then looks for a boss using 'getOneObjectAtOffset' -- but, from where is this offset to be taken (if the bullet is no longer in the world, there is no offset to be had). Change line 29 to:
if (getWorld() != null) hitBoss();
This will ensure that the bullet is in the world before calling 'hitBoss'.
Jillis Jillis

2017/3/23

#
oh yes, I just did the same with the normal enemy too, thank you. I also receive this when the user dies. Do I need to put the same thing before each function is called in the user class too? User code:
{
    boolean nottouchingenemy = false;
    boolean nottouchingboss = false;
    public void act() 
    {
        String key = Greenfoot.getKey();
        if (Greenfoot.isKeyDown("a"))
        {
            setRotation(180);
            if (GetObjectInFront(Wall.class)==null)
            move(8);
        }
        if (Greenfoot.isKeyDown("d"))
        {
            setRotation(0);
            if (GetObjectInFront(Wall.class)==null)
            move(8);
        }
        if (Greenfoot.isKeyDown("w"))
        {
            setRotation(270);
            if (GetObjectInFront(Wall.class)==null)
            move(8);
        }
        if (Greenfoot.isKeyDown("s"))
        {
            setRotation(90);
            if (GetObjectInFront(Wall.class)==null)
            move(8);
        }
        if ("left".equals(key))
        {
            setRotation(180);
            fire();
        }
        if ("right".equals(key))
        {
            setRotation(0);
            fire();
        }
        if ("down".equals(key))
        {
            setRotation(90);
            fire();
        }
        if ("up".equals(key))
        {
            setRotation(270);
            fire();
        }
        hitEnemy();
        hitBoss();
        enterdoor();
    }
    private void fire()
    {
        Bullet bullet = new Bullet();
        getWorld().addObject(bullet, getX(), getY());
        bullet.setRotation(getRotation());
        bullet.move(30);
    }
    private Actor GetObjectInFront(Class c)
    {
        GreenfootImage myImage = getImage();
        int DistanceFront = myImage.getWidth();
        int yOffset = (int)Math.ceil(DistanceFront*Math.sin(Math.toRadians(getRotation())));
        int xOffset = (int)Math.ceil(DistanceFront*Math.cos(Math.toRadians(getRotation())));
        return (getOneObjectAtOffset(xOffset, yOffset, c));
    }
    public void hitEnemy()
    {
        Actor enemynormal = getOneIntersectingObject(EnemyNormal.class);
        if(enemynormal != null)
        {
            if(nottouchingenemy == false)
            {
                setImage("redperson.png");
                World myWorld = getWorld();
                World1 world1 = (World1)myWorld;
                Healthbar healthbar = world1.getHealthBar();
                healthbar.loseHealth();
                Greenfoot.delay(10);
                nottouchingenemy = true;
            }else {
                nottouchingenemy = false;
                setImage("person.png");
            }
        }
    }
    public void hitBoss()
    {
        Actor boss = getOneIntersectingObject(Boss.class);
        if(boss != null)
        {
            if(nottouchingboss == false)
            {
                setImage("redperson.png");
                World myWorld = getWorld();
                World1 world1 = (World1)myWorld;
                Healthbar healthbar = world1.getHealthBar();
                healthbar.loseHealth();
                Greenfoot.delay(10);
                nottouchingboss = true;
            }else {
                nottouchingboss = false;
                setImage("person.png");
            }
        }
    }
    public void enterdoor()
    {
      //code
    }
}
danpost danpost

2017/3/23

#
Jillis wrote...
I also receive this when the user dies. Do I need to put the same thing before each function is called in the user class too? < Code Omitted >
I do not think so -- I do not see where the user is removing itself from the world.
You need to login to post a reply.