I have spent several hours coding this game, and I watch a video to try to implement health bar.. and OMG it will not work. All it does in the game is take damage but the bar doesn't go down! after taking damage (I think it doesent update properly). So far I have this
this code is used for my main character
public void Health1()
{
Actor asteroid = getOneIntersectingObject(Asteroid.class);
if (asteroid != null)
{
World myWorld = getWorld();
Space space = (Space)myWorld;
HealthBar0 healthbar0 = space.getHealthBar0();
if(touchingAsteroid == false)
{
Greenfoot.playSound("Impact.wav");
touchingAsteroid = true;
healthbar0.loseHealth0();
if (healthbar0.health <=0)
{
Greenfoot.playSound("ExplotionShip.wav");
getWorld().showText("You Died!",500,300);
myWorld.removeObject(this);
Greenfoot.stop();
}
}
} else {
touchingAsteroid = false;
}
}
This is the code for the Health (which is an actor)
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class HealthBar0 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class HealthBar0 extends Actor
{
int health = 5;
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 HealthBar0()
{
updateHealth();
}
public void act()
{
updateHealth();
}
public void updateHealth()
{
setImage (new GreenfootImage(healthBarWidth + 2, healthBarHeight + 2));
GreenfootImage myImage = getImage();
myImage.setColor(greenfoot.Color.WHITE);
myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1);
myImage.setColor(greenfoot.Color.RED);
myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeight);
}
public void loseHealth0()
{
health--;
}
}
this is what i have in my World Class
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Space here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Space extends World
{
HealthBar0 healthbar0 = new HealthBar0();
/**
* Constructor for objects of class Space.
*
*/
public Space()
{
super(1000, 600, 1);
addCharacters();
}
public HealthBar0 getHealthBar0()
{
return healthbar0;
}
public void addCharacters()
{
addObject (new Ship(), 30, 300);
addObject (new HealthBar0(), 50, 70);
addObject (new Asteroid(), 400, 500);
addObject (new Asteroid(), 800, 200);
addObject (new Asteroid(), 600, 400);
addObject (new Asteroid(), 500, 200);
}
public void act()
{
if (Greenfoot.isKeyDown("p")) {
Greenfoot.setWorld( new PauseScreen());
}
}
}
I am a noob at greenfoot so can you help me solve this problem ASAP.(and prob reply with my code but fixed)


