This site requires JavaScript, please enable it in your browser!
Greenfoot back
pandu4431
pandu4431 wrote ...

2015/4/30

making health bar [HELP] please

pandu4431 pandu4431

2015/4/30

#
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;
            }
        }
    }
    
}
azazeel azazeel

2015/4/30

#
so what is the problem ?
azazeel azazeel

2015/4/30

#
i mean where the code loseHealth() ?
danpost danpost

2015/4/30

#
The problem is that no code has been added to apply conditions to setting the 'touchingAsteroid' field back to 'false'.
pandu4431 pandu4431

2015/4/30

#
oh sure, thanks a lot guys, you are the best
You need to login to post a reply.