I have used this specific code for my health bar, and it is working, however, i want it to take 1 pixel of health away each time the rocket hits my plane. At the moment, the health bar decreases immediately when the rocket hits me. HELP!!
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class HealthBar here.
*
* @author (Baris Yildirim)
* @version (Greenfoot version 2.4.0)
*/
public class HealthBar extends bar
{
int health = 3; //The amount of lifes which the Car has before dying
int healthBarWidth = 150; //The Width of the Health Bar
int healthBarHeight = 15; //The Height of the Health Bar
int pixelsPerHealthPoint = (int)healthBarWidth/health; //This Coding divides the Width by the Health
public void act()
{
update();
//loseHealth();
}
public HealthBar()
{
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--;
update();
if (health<1)
{
getWorld().addObject(new GameOver(),getWorld().getWidth()/2,getWorld().getHeight()/2);
getWorld().addObject(new Restart(), 505,365);
update();
//Greenfoot.stop();
//Greenfoot.delay();
}
}
}
