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

2016/5/15

Make a healthbar deteriorate over time

BearProject BearProject

2016/5/15

#
How would I make a healthbar deteriorate over time? Am I required to make a separate class for a clock?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class HealthBar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HealthBar extends ScrollingActor
{
    /**
     * Act - do whatever the HealthBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int health = 100;
    int healthBarWidth = 100;
    int healthBarHeight = 30;
    int pixelsPerHealthPoint = (int) healthBarWidth/health; 

    public HealthBar(){
        act();
    }

    public int getHealth()
    {
        return health;
    }

    public void act() 
    {
        update();
        if (Greenfoot.isKeyDown("up")) {
            setLocation(getX(), getY()-5); 
        }
        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(), getY()+5);
        }
        if (Greenfoot.isKeyDown("left")) {
            setLocation(getX()-5, getY());
        }
        if (Greenfoot.isKeyDown("right")) {
            setLocation(getX()+5, getY());
        }

    }

    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.GREEN);
        myImage.fillRect(1, 1, health * pixelsPerHealthPoint, healthBarHeight);
    }

    public void loseHealthPoisonousBerries(){
        health-=7;
    }

    public void addHealthHive(){
        if(health<=85){
            health+=15;
        }
        else{
            health=100;
        }
    }

    public void loseHealthBees(){
        health-=50;
    }

    public void addHealthBerries(){
        if(health <= 95){
            health+=5;
        }
        if(health == 96){
            health+=4;
        }
        if(health == 97){
            health+=3;
        }
        if(health == 98){
            health+=2;
        }
        if(health == 99){
            health+=1;
        }
    }

    public void addHealthFish(){
        if(health <=93){
            health+=7;
        }
        if(health == 94){
            health+=6;
        }
        if(health == 95){
            health+=5;
        }
        if(health == 96){
            health+=4;
        }
        if(health == 97){
            health+=3;
        }
        if(health == 98){
            health+=2;
        }
        if(health == 99){
            health+=1;
        }
    }

}
Meh Meh

2016/5/15

#
just make a variable that increaes after each act. If the variable has a certain value decrease the variable health and return the variable
BearProject BearProject

2016/5/15

#
I did it like this but this only makes the health bar deteriorate in the beginning of the game and does not further deteriorate after that
public class HealthBar extends ScrollingActor
{
    /**
     * Act - do whatever the HealthBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int health = 100;
    int healthBarWidth = 100;
    int healthBarHeight = 30;
    int pixelsPerHealthPoint = (int) healthBarWidth/health; 
    int increasingTime = 0;

    public HealthBar(){
        act();
    }

    public int getHealth()
    {
        return health;
    }

    public void act() 
    {
        update();

        if (Greenfoot.isKeyDown("up")) {
            setLocation(getX(), getY()-5); 
        }
        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(), getY()+5);
        }
        if (Greenfoot.isKeyDown("left")) {
            setLocation(getX()-5, getY());
        }
        if (Greenfoot.isKeyDown("right")) {
            setLocation(getX()+5, getY());
        }

        increasingTime++;
        if(increasingTime ==5){
            health-=5;
        }

    }
danpost danpost

2016/5/16

#
Using a limit of '5' for 'increasingTime' is a very short time (about 1/10th of a second). You will probably want to increase its value a bit (maybe use '50') and only decrement health by one instead of '5'. When decrementing the health value, you need to reset the value of 'increasingTime' back to zero.
BearProject BearProject

2016/5/16

#
Thank you so much!
You need to login to post a reply.