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

2015/9/6

"Actor not in World"

Vellyxenya Vellyxenya

2015/9/6

#
Hello, I created a method in the Monsters.class which deals damages to the Gear.class. In the Gear.class i created a method that lower the gear's life when it takes damage from a monster (by touching it), and when its life is 0, it should be removed. Here is the code of the Monsters class
private static final int damage = 5;
private void damageGears()
    {
        [b]Gear1 gear = (Gear1) getOneIntersectingObject(Gear1.class);[/b]
        if (gear != null)
        {
            gear.takeDamage(damage);
        }
    }
and here's the gear's code:
private static int Life = 3000;
public void takeDamage(int damage)
    {
        Life = Life - damage;
        if(Life <=0)
        {
            dead = true;
            getWorld().removeObject(this);
        }
    }
The problem is that, when i play and my gear is out of health and it should die, the greenfoot terminal window appears and tell me: "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." and shows that the fault is in the line that i wrote in bold... I think that when the Gear is removed, the Monster's class doesn't find the monster that it's meant to damage... but how to deal with that? Thank you ^^
danpost danpost

2015/9/6

#
The 'damageGear' method is being called while the monster is not in the world. You probably are calling a method that is removing it (maybe when it reaches an edge of the world or something) before calling this method. To prevent calling one method after another removes the actor from the world, qualify the calling of the method with:
if (getWorld() != null) damageGear();
Vellyxenya Vellyxenya

2015/9/7

#
I've been trying to do:
if (Gear1 != null) damageGear();
or
if(Gear1() != null) damageGear();
but both didn't work, i've managed to have the same result another way (by taking the damage only from the gear class) but, gonna think about the (getWorld() != null) next time, thank you!
You need to login to post a reply.