The Healthbar won't go down when X class touches Y class or rather the bar isn't updating.
Healthbar
X class
Can anyone help?
int health = 4;
int healthWidth = 80;
int healthHeight = 15;
int pixelsPerHealthPoint = (int)healthWidth/health;
public Healthbar()
{
update();
}
public void update()
{
setImage(new GreenfootImage(healthWidth + 2, healthHeight + 2));
GreenfootImage myImage = getImage();
myImage.setColor(Color.WHITE);
myImage.drawRect(0, 0, healthWidth + 1, healthHeight + 1);
myImage.setColor(Color.RED);
myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthHeight);
}
public void loseHealth()
{
health--;
update();
}boolean touchingY = false;
public void act()
{
hit();
}
public void hit()
{
Actor y = getOneIntersectingObject(Y.class);
if(y != null)
{
World myWorld = getWorld();
MyWorld myworld = (MyWorld)myWorld;
Healthbar healthbar = myworld.getHealthBar();
if(touchingY == false)
{
healthbar.loseHealth();
touchingY = true;
if(healthbar.health <= 0)
{
getWorld().removeObject(this);
}
}
}
else
{
touchingY = false;
}
}


