This is the healthBar code.
This Part is on the Player Class.
And i almost forgot but on my world i got
The problem here is when the enemy touches the player nothing happens health does not go down and i can't really figure it out why. i thing i got all the relevent pieces of code pasted here.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
public class healthBar extends Chars
{
int health = 4;
int healthBarWidth = 80;
int healthBarHeight = 15;
int pixelsPerHealthPoint = (int)healthBarWidth/health;
public healthBar()
{
update();
}
public void act()
{
update();
}
public void update()
{
setImage(new GreenfootImage(healthBarWidth +2, healthBarHeight +2));
GreenfootImage image = getImage();
image.setColor(Color.WHITE);
image.drawRect(0 , 0 , healthBarWidth + 1, healthBarHeight + 1);
image.setColor(Color.RED);
image.fillRect(1,1,health*pixelsPerHealthPoint, healthBarHeight);
}
public void loseHealth()
{
health--;
}
}boolean touchingEnemyAi = false;
public void hitBanana()
{
Actor banana = getOneIntersectingObject(EnemyAi.class);
if(banana != null)
{
World myWorld = getWorld();
Cave cave = (Cave)myWorld;
healthBar HP = cave.gethealthBar();
if(touchingEnemyAi == false)
{
HP.loseHealth();
touchingEnemyAi = true;
if(HP.health <= 0)
{
myWorld.removeObject(this);
}
}
}
else
{
touchingEnemyAi = false;
}
}healthBar HP = new healthBar();
public healthBar gethealthBar()
{
return HP;
}

