So I've created a health bar and have set everything up so that it should work but when my character encounters an enemy nothing happens. I think it is because of the code in the health bar class because when I just have it preform the remove code action on its own nothing happens.
public class HealthBar extends Actor
{
int health = 4;
int healthBarWidth = 80;
int healthBarHeight = 15;
int pixelsPerHealthPoint = (int)healthBarWidth/health;
/**
* Act - do whatever the HealthBar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public HealthBar()
{
update();
}
public void act()
{
update();
}
public void update()
{
setImage(new GreenfootImage(healthBarWidth + 2, healthBarHeight + 2));
GreenfootImage myImage = getImage();
myImage.setColor(Color.WHITE);
myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1);
myImage.setColor(Color.RED);
myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeight);
}
public void loseHealth()
{
health--;
}
}public class Lavnar extends Mover
{
boolean touchingSamus = false;
/**
* Act - do whatever the lavnar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
lookForSamus();
followSamus();
killSamus();
}
public void lookForSamus()
{
if (isAtEdge())
{
turn(180);
}
}
public void killSamus()
{
if (isTouching(Samus.class))
{
World myWorld = getWorld();
LavaLand lavaLand = (LavaLand)myWorld;
HealthBar healthbar = lavaLand.getHealthBar();
if(isTouching(Samus.class) == false)
{
healthbar.loseHealth();
touchingSamus = true;
if(healthbar.health <=0)
{
removeTouching(Samus.class);
Greenfoot.stop();
LavaLand w = (LavaLand) getWorld();
w.showEndMessage();
}
}
} else {
touchingSamus = false;
}
}
public void followSamus()
{
int dist = 1000;
Mover closest = null;
if(!getObjectsInRange(dist, Samus.class).isEmpty())
{
for (Object obj: getObjectsInRange(dist, Samus.class))
{
Mover Samus = (Mover) obj;
move(4);
int SamusDist = (int) Math.hypot(Samus.getX() - getX(), Samus.getY() - getY());
if (closest == null || SamusDist< dist)
{
closest = Samus;
dist = SamusDist;
}
}
turnTowards(closest.getX(),closest.getY());
}
}
}public class LavaLand extends World
{
ScoreCounter scoreCounter = new ScoreCounter();
HealthBar healthBar = new HealthBar();
GreenfootSound backgroundMusic = new GreenfootSound("sandstorm.mp3");
/**
* Constructor for objects of class LavaLand.
*
*/
public LavaLand()
{
super(800, 600, 1);
showName();
showControl();
Samus mySamus = new Samus();
addObject(mySamus, 400, 300);
addObject(scoreCounter, 100, 25);
addObject(healthBar, 200, 25);
}
public ScoreCounter getScoreCounter()
{
return scoreCounter;
}
public HealthBar getHealthBar()
{
return healthBar;
}
}

