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

2015/9/16

health bar

hoothoot hoothoot

2015/9/16

#
alright i figured it out, i just need to know how to decrease the "bar"'s value as my characters health goes down.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.Font;
/**
 * Write a description of class SomeWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CellGames extends World
{
    Bar bar = new Bar("Cell", "HP", 900000, 900000);
    Cell cell = new Cell();
    int cHlth = cell.getHealth();
    /**
     * Constructor for objects of class SomeWorld.
     * 
     */
    public CellGames()
    {
        super(900, 650, 1);
        prepare(); 
        addObject(bar, 600, 10);
        addObject(cell, 600, 390);
        setbar();
    }
    private void prepare ()
    {
        Gohan gohan = new Gohan();
        addObject(gohan, 200, 450);
        Fight fight = new Fight();
        addObject(fight, 400, 300);           
    }
    public void setbar(){
        if (bar.getValue() == bar.getMinimumValue())
        {
        }
        if (cHlth <= 900000)
        {
            bar.setValue(-1500);
        }
    }
}
ignore the setbar because that's what I need to know, how do I get it to detect my characters health and change to that value?
danpost danpost

2015/9/16

#
The healthbar does not need to detect changes in the health value of the character -- the character can tell the healthbar to adjust its value when needed. Anytime it needs to set/adjust the bar value, it can use something like the following:
((CellWorld)getWorld()).bar.setValue(health);
Easier, however, would be to place all codes related to the healthbar in the Gohan class to begin with. Follow my tutorial for code in displaying a value in an Actor subclass.
hoothoot hoothoot

2015/9/16

#
alright thanks danpost
You need to login to post a reply.