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

2021/3/1

Health Bar

dan.0071 dan.0071

2021/3/1

#
Hi, Im trying to make a healthbar for a game that im making for a project and having problems with creating the image as well as the health bar to work the correct way. For example, when the player gets it by a bullet the health goes down and when the player upgrades their armour/health it goes up. Here is what I have so far:
mport greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

public class healthBar extends Player
{
    private int maxHealth = 100;//GREEN
    private int warningHealth = 40;//ORANGE
    private int lowHealth = 20;//RED
    private int endHealth = 0; //player eliminated
    private int maxValue = 0; 
    private int minValue = 0;
    private int value; //current state of health

    private int barWidth = 100; //Width of Player's Health Bar
    private int barHeight = 10; //Height of Player's Health Bar
    

    private boolean colorChange = false; //
    private boolean changeLowHealth = true;
    private Color textColor = Color.BLACK; // shows value of how much health the player has
    private Color greenHealth = Color.GREEN; // Health color til health gets down to 40
    private Color orangeHealth = Color.ORANGE;// Health color til health gets down to 20
    private Color redHealth = Color.RED;// Health color til player is destroyed.
   
    private void newImage()
    {
        int value = (int)(barWidth * (value - minValue) / (maxValue - minValue));// values to adjust when player is hit or upgrades health
        Greenfoot heathBarImg = new GreenfootImage(barWidth,barHeight);

        if (value > 100, value >= 41)
        {
            heathBarImg.setColor(greenHealth);
        }
        else if (value <=40, value >= 21)
        {
            heathBarImg.setColor(orangeHealth);
        }
        else 
        {
            healthBarImg.setColor(redHealth)
        }
    }

    public void hit(int amount)
    {
        value -= amount;
        checkValue();
    }

    public void addHealth(int amount)
    {
        value += amount;
        checkValue();
    }

    private void checkValue()
    {
        if (value < endHealth) value = endHealth;
        if (value > maxHealth) value = maxHealth;
    }

    public int getValue()
    {
        return value;
    }
}
Can anyone help? :/
danpost danpost

2021/3/1

#
Remove line 2. At end of checkValue, call newImage.
dan.0071 dan.0071

2021/3/4

#
This makes creates errors with the health bar changing colour. I've tried looking and compared your scenario on your healthBar system and didn't have any luck.
danpost danpost

2021/3/4

#
dan.0071 wrote...
This makes creates errors with the health bar changing colour. I've tried looking and compared your scenario on your healthBar system and didn't have any luck.
On lines 30 and 34, you are using a comma where you need a conditional operator ("||" is OR and "&&" is AND).
dan.0071 dan.0071

2021/3/5

#
All sorted now thank you :)
You need to login to post a reply.