So i don't see anything wrong with this code for my health bar but when i run the scenario and i shoot the boss, it gives me an error saying that an actor isn't in the world when I'm trying to call on it. This is the code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color; // Need to for fill color of health bar
/**
* Write a description of class HealthBar here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Boss2HealthBar extends HealthBars
{
private int health = 100; // Say, they start with 100 health
public Boss2HealthBar()
{
}
protected void addedToWorld(World w)
{
updateHealthBar(0);
}
public int updateHealthBar(int healthChange)
{
health = health - healthChange; // Note: you can add health by passing in a negative change
if(health <= 0 )
{
return 0;
}
else if(health > 100 )
{
health = 100; // can't have more than the max health
}
// Redraw health bar to match current health
GreenfootImage hb = new GreenfootImage(health, 20);
hb.setColor(Color.GREEN);
hb.fill();
setImage(hb);
setLocation(getImage().getWidth()/2 +250, getY());
return health;
}
public int health()
{
return health;
}
}

