hey guys, i tried to make health bar in my greenfoot games.
my problem is when the enemy hit y main actor, the health bar just decrease in first hit, and when the main actor hit again, health bar didn't decrease again.
can anyone help me?
i create the health bar by following tutorial from :
this my code for my actor, i think there was a problem
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* A simple SpaceShip that can move up and down.
*
* @author Poul Henriksen
*/
public class SpaceShip extends Actor
{
private int shotTimer =0;
boolean touchingAsteroid = false;
public void act()
{
if (Greenfoot.isKeyDown("up") ) {
setLocation (getX(), getY() - 4);
}
if (Greenfoot.isKeyDown("down") ) {
setLocation (getX(), getY() + 4);
}
if (Greenfoot.isKeyDown("left")) {
setLocation(getX() - 5, getY());
}
if (Greenfoot.isKeyDown("right")) {
setLocation(getX() + 5, getY());
}
if (shotTimer > 0) {
shotTimer = shotTimer - 1;
}
else if (Greenfoot.isKeyDown("space")) {
getWorld().addObject(new Shot(this), getX(), getY());
shotTimer = 20; // delay next shot
}
hitAsteroid();
}
public void hitAsteroid(){
Actor asteroid = getOneIntersectingObject(Asteroid.class);
if(asteroid != null){
World myWorld = getWorld();
ScrollWorld space = (ScrollWorld)myWorld;
HealthBar healthbar = space.getHealthBar();
if(touchingAsteroid == false){
healthbar.loseHealth();
touchingAsteroid=true;
}
}
}
}
