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

2020/3/11

healthbar that changes color when hp hits certain percent

genju genju

2020/3/11

#
im making a modular healthbar class that changes the display color based on the health of the player/target. if the bar health is above 75% it is green, between 35 and 74 its orange and if its below 35 then its red. It automatically sets to whichever color is set in the constructor instead of following my update method here is the code so far pls help
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * The Health Bar class is one that helps the user track the
 * overall hp of the given target 
 * 
 * @author Prayan Jegathees 
 * @version March 2020
 */


public class PJHpBar extends Actor
{
    // instance variables
    private int hp;
    
    // 
    private int value;
    private int minimumVal = 0;
    private int maximumVal;
    private int minValue;
    private int maxValue;
    
    // HP bar dimensions
    private int hpBarWidth = 140;
    private int hpBarHeight = 24;
    
    // image variable names
    private GreenfootImage bar;
    private GreenfootImage border;
    
    // bar colors
    private Color healthyColor = Color.GREEN; 
    private Color midColor = Color.ORANGE;
    private Color dangerColor = Color.RED;
    
    // chunk removed from HP bar when health is decreased
    private int hpPerHit;
    
    // target for HP bar to follow & how far above to attach the bar
    private Actor target;
    private int offset; 
    
    /**
     * Default Constructor for the PJHpBar class, creating a functioning HP Bar
     * 
     * @param hp            takes in a value for the amount of HP the target has and what to display on the HP Bar
     * @param maximumVal    takes in a value for the maximum value of the bar
     * 
     */
    public PJHpBar(int hp,int maximumVal)
    {
        bar = new GreenfootImage(hpBarWidth, hpBarHeight);
        bar.setColor(healthyColor);
        bar.fill();
        
        bar.setColor (Color.BLACK);
        bar.drawRect (0, 0, hpBarWidth - 2, hpBarHeight -2);
        
        this.setImage(bar);
               
        minValue = minimumVal;
        maxValue = maximumVal;
        this.hp = hp;
        hpPerHit  = (int) ((double)hpBarWidth / hp);
    }
    
    /**
     * Act - do whatever the PJHealthBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        update();
    }    
    
    /**
     * Update - adjust bar hp color based on the current hp of the target
     * <p>
     *  - Green if the target's health is between 100% and 75%
     * <p>
     *  - Orange if the target's health is between 35% and 74%
     * <p>
     *  - Red if the target's health is below 35%
     */
    public void update()
    {
        int percentHealth = ((hp / maxValue) * 100);
        if (percentHealth >= 75)
        {
            bar.setColor(healthyColor);
        }
        if (35 <=  percentHealth && percentHealth <= 74)
        {
            bar.setColor(midColor);
        }
        if (0 <= percentHealth && percentHealth <= 34)
        {
            bar.setColor(dangerColor);
        }
    }
    
    /**
     * Add - add an amount to the value of the bar, checks to make sure the new value is between minimumValue and maximumValue,
     * then, calls 'newImage' to build and set the new image for the bar.
     *
     * @param amount - the amount to add (if not negative) or subtract (if negative) to the current value of the bar
     */
    public void add(int amount)
    {
        value += amount;
        protectValue();
    }
    
    /**
     * Subtract - subtracts an amount from the value of the bar, checks to make sure the new value does not overstep its bounds,
     * then, calls 'updBarImage' to build and set the new image for the bar.
     *
     * @param amount - the amount to subtract (if positive) or add (if negative) to the current value of the bar
     */
    public void loseHP(int amount)
    {
        value -= amount;
        protectValue();
    }
    
    /**
     * Method protectValue - ensures that the new value in between the minimum value and the maximum value for the bar
     */
    private void protectValue()
    {
        if (value < minValue) 
        {
            value = minValue;
        }
        
        if (value > maxValue) 
        {
            value = maxValue;
        }
    }
}
danpost danpost

2020/3/11

#
Double check your inequality operators (lines 92 and 96).
genju genju

2020/3/11

#
danpost wrote...
Double check your inequality operators (lines 92 and 96).
i dont see anything wrong with them, i feel like i have to use the update method on an instance variable that automatically sets the color for the constructor but i dont know how to do that
danpost danpost

2020/3/12

#
genju wrote...
i dont see anything wrong with them,
You are correct. I was looking at them incorrectly.
i feel like i have to use the update method on an instance variable that automatically sets the color for the constructor but i dont know how to do that
Well -- sort of. First, change the name of your update method to setColor (since that is all it does. Then, replace healthyColor in the constructor with setColor(). Next, create a new empty method called update and move the first 6 lines in the constructor into this new method, replacing where they came from with update(). Finally, remove the act method totally and put update() at the end of both the add and loseHP methods.
genju genju

2020/3/12

#
I did everything you said to do leaving me with
    public PJHpBar(int hp,int maximumVal)
    {
        update();
        
        this.setImage(bar);
        minValue = minimumVal;
        maxValue = maximumVal;
        
        this.hp = hp;
        hpPerHit  = (int) ((double)hpBarWidth / hp);
        setColor();
    }
    
    /**
     * setColor - adjust bar hp color based on the current hp of the target
     * <p>
     *  - Green if the target's health is between 100% and 75%
     * <p>
     *  - Orange if the target's health is between 35% and 74%
     * <p>
     *  - Red if the target's health is below 35%
     */
    public void setColor()
    {
        int percentHealth = ((hp / maxValue) * 100);
        if (percentHealth >= 75)
        {
            bar.setColor(healthyColor);
        }
        if (35 <=  percentHealth && percentHealth <= 74)
        {
            bar.setColor(midColor);
        }
        if (0 <= percentHealth && percentHealth <= 34)
        {
            bar.setColor(dangerColor);
        }
    }
    
    public void update()
    {
        bar = new GreenfootImage(hpBarWidth, hpBarHeight);
        bar.setColor(setColor());
        bar.fill();
        
        bar.setColor (Color.BLACK);
        bar.drawRect (0, 0, hpBarWidth - 2, hpBarHeight -2);
    }
    
    /**
     * Add - add an amount to the value of the bar, checks to make sure the new value is between minimumValue and maximumValue,
     * then, calls 'newImage' to build and set the new image for the bar.
     *
     * @param amount - the amount to add (if not negative) or subtract (if negative) to the current value of the bar
     */
    public void addHP(int amount)
    {
        value += amount;
        protectValue();
        update();
    }
    
    /**
     * Subtract - subtracts an amount from the value of the bar, checks to make sure the new value does not overstep its bounds,
     * then, calls 'updBarImage' to build and set the new image for the bar.
     *
     * @param amount - the amount to subtract (if positive) or add (if negative) to the current value of the bar
     */
    public void loseHP(int amount)
    {
        value -= amount;
        protectValue();
        update();
    }
    
    /**
     * Method protectValue - ensures that the new value in between the minimum value and the maximum value for the bar
     */
    private void protectValue()
    {
        if (value < minValue) 
        {
            value = minValue;
        }
        
        if (value > maxValue) 
        {
            value = maxValue;
        }
    }
yet in the update() method, where you told me to change healthyColor to setColor() is an error underline under the parentheses and says 'void' type not allowed here
public void update()
    {
        bar = new GreenfootImage(hpBarWidth, hpBarHeight);
        bar.setColor(setColor());
        bar.fill();
        
        bar.setColor (Color.BLACK);
        bar.drawRect (0, 0, hpBarWidth - 2, hpBarHeight -2);
    }
danpost danpost

2020/3/12

#
genju wrote...
I did everything you said to do leaving me with << Code Omitted >> yet in the update() method, where you told me to change healthyColor to setColor() is an error underline under the parentheses and says 'void' type not allowed here << Code Omitted >>
Oops. Replace line 43 with the code in setColor and then remove the setColor method. Also line 5 (the setImage line in the constructor) should have been moved along with the other lines (should be at end of update method now).
genju genju

2020/3/12

#
now there is an error within the update method and where i added the update method in the constructor.. java.lang.ArithmeticException: / by zero at PJHpBar.update(PJHpBar.java:64) at PJHpBar.<init>(PJHpBar.java:51)
public PJHpBar(int hp,int maximumVal)
    {
        update();
        
        minValue = minimumVal;
        maxValue = maximumVal;
        
        this.hp = hp;
        hpPerHit  = (int) ((double)hpBarWidth / hp);
    }
    
    public void update()
    {
        bar = new GreenfootImage(hpBarWidth, hpBarHeight);
        
        int percentHealth = ((hp / maxValue) * 100);
        if (percentHealth >= 75)
        {
            bar.setColor(healthyColor);
        }
        if (35 <=  percentHealth && percentHealth <= 74)
        {
            bar.setColor(midColor);
        }
        if (0 <= percentHealth && percentHealth <= 34)
        {
            bar.setColor(dangerColor);
        }
        
        bar.fill();
        bar.setColor (Color.BLACK);
        bar.drawRect (0, 0, hpBarWidth - 2, hpBarHeight -2);
        
        this.setImage(bar);
    }
danpost danpost

2020/3/12

#
Move line 3 to AFTER line 9. Also, remove everything dealing with hpPerHit. Then, so as not to lose all precision, change line 16 to:
int percentHealth = hp*100/maxValue;
(as you had it, the only two percentage results would be 100 and 0) Finally, line 30 must fill a rectangle based on that percentage, provided that the percent value is not zero.
genju genju

2020/3/12

#
danpost wrote...
Finally, line 30 must fill a rectangle based on that percentage, provided that the percent value is not zero.
bar.fill(percentHealth) doesnt work so im not sure exactly how i'd do that
danpost danpost

2020/3/12

#
genju wrote...
bar.fill(percentHealth) doesnt work so im not sure exactly how i'd do that
Its something like:
bar.fillRect(0, 0, hpBarWidth()*100/percentHealth, hpBarHeight);
genju genju

2020/3/12

#
danpost wrote...
genju wrote...
bar.fill(percentHealth) doesnt work so im not sure exactly how i'd do that
Its something like:
bar.fillRect(0, 0, hpBarWidth()*100/percentHealth, hpBarHeight);
thanks for the help!
You need to login to post a reply.