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

2017/3/16

Error when removing an enemy from the world

Jillis Jillis

2017/3/16

#
When my enemy has been hit by a bullet 5 times, I want it to be removed from the world. However, when my enemy is removed I receive the error saying that it cannot remove something that is not there. I am aware that this means that the remove object is being called even after the enemy has died, but I can't work out how to stop this. The following is the code for my enemy and then the bullet:
public class EnemyNormal extends Actor
{
    private int health = 100;
    /**
     * Act - do whatever the EnemyNormal wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void setHealth (int points)
    {
        health += points;
    }
    public int getHealth()
    {
        return health;
        if  (health <= 0)
        {
            getWorld().removeObject(this);
        }
    }
    public void act()
    {
        move(4);
        if (Greenfoot.getRandomNumber(100) < 10)
        {
            turn(Greenfoot.getRandomNumber(90) - 45);
        }
        if (getX() <= 100 || getX() >= getWorld().getWidth() - 100)
        {
            turn(180);
        }
        if (getY() <= 100 || getY() >= getWorld().getWidth() - 100)
        {
            turn(180);
        }
    }
}
{
    /**
     * 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 act() 
    {
        move(10);
        hitEnemy();
    }
}
danpost danpost

2017/3/16

#
You have code in the 'getHealth' method that will never be executed. A 'return' of any kind will exit the method immediately leaving any remaining code beyond it to be left unexecuted. See if you can find the appropriate place for lines 15 through 18 in the EnemyNormal class. HINT: the checking of the value of a variable only needs to be done when that value is changed.
Jillis Jillis

2017/3/21

#
Hi sorry for a long reply, just a follow up on this. I can't seem to find where to put those lines? I tried to place them initially before and then after line 10 so that when the health is being changed it is checked. However this didn't seem to work. Perhaps the health of the spider is not changing?
Super_Hippo Super_Hippo

2017/3/21

#
Show what you have now, because it should be placed after 10.
Jillis Jillis

2017/3/21

#
I never called the getHealth() in the act method! It works perfectly now :)
Super_Hippo Super_Hippo

2017/3/21

#
Well, there is no need to call it from the act method anymore if you moved 15-18 to after 10.
You need to login to post a reply.