I cant seem to figure this out here is my code for my health bar and the actor i want to take damage
public void hitBullet()
{
Actor Bullet = getOneIntersectingObject(Bullet.class);
if(Bullet != null)
{
World myWorld = getWorld();
Hoth hoth = (Hoth)myWorld;
HealthBar healthbar = hoth.getHealthBar();
if(touchingBullet == false)
{
healthbar.loseHealth();
touchingBullet = true;
if(healthbar.health <=0)
{
myWorld.removeObject(this);
}
} else {
touchingBullet = false;
}
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class HealthBar here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class HealthBar extends Actor
{
int health = 35;
int healthBarWidth = 100;
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--;
}
}

