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

2017/1/30

Healthbar Bugs

Theangryman Theangryman

2017/1/30

#
Hey guys, I'm making a character design screen for a game I'm working on and one of the UI elements is a health bar that should update depending on the values in one of its superclasses; so far the healthbar is capable of sliding up and down dependant on the value given to it from the world constructor, but I really don't want it to take the integer I want it to display from the constructor to make life in the future easier. Whenever I set the value to anything above 100, it instantly starts dropping to 0 (unintended) and whenever I set the value to 100 or below the bar doesn't change the display. Here's the line in the World constructor that builds the healthbar:
ProgBar health = new ProgBar(100, 300, 20, "Health", 90, 240, 7);
Here's my code for the health bar:
public class ProgBar extends CellStats
{
    public int curVar;
    public int barWidth;
    public int barHeight;
    public int pxPerHealthPoint;
    public String barLabel;
    public int RED;
    public int GREEN;
    public int BLUE;
    
    public ProgBar(int Fraction, int Width, int Height, String barLbl, int r, int g, int b)
    {   
        curVar = Fraction;
        barWidth = Width;
        barHeight = Height;
        barLabel = barLbl;
        RED = r;
        GREEN = g;
        BLUE = b;
        pxPerHealthPoint = (int)barWidth/curVar;
        updateBar();
    }
    public void act() 
    {
        updateBar();
    }    
    public void updateBar()
    {
       //draws the progress bar
       setImage(new GreenfootImage(160+ (barWidth + 4), barHeight + 4));
       GreenfootImage barBg = getImage();
       barBg.setColor(new Color (100, 100, 100));
       barBg.fillRect(160, 0, barWidth + 4, barHeight + 4);
       barBg.setColor(new Color(RED, GREEN, BLUE));
       barBg.fillRect(162, 2, curVar*pxPerHealthPoint, barHeight);
       
       barBg.setFont(barBg.getFont().deriveFont(20f));
       barBg.setColor(new Color(255, 255, 255));
       barBg.drawString(barLabel, 1, 20);
       barBg.drawString(String.valueOf(curVar), 425, 19);
       
       int currentHealth = CellStats.returnValues();
       if(currentHealth > curVar && curVar > 0)
       {
           loseValue();
       }
    }
    public void gainValue()
    {
        curVar++;
    }
    public void loseValue()
    {
        curVar--;
    }
}
Here's my code for the CellStats superclass:
public class CellStats extends Actor
{
    static int currentHealth = 100;
    int baseHealth = 100;
    int heatRes = 0;
    int coldRes = 0;
    int shield = 0;
    int atpCap = 300;
    int atpStored = 0;
    
    
    public CellStats() 
    {
        returnValues();
    }    
    public static int returnValues()
    {
        return currentHealth;
    }
Tell me where I'm being a complete noob, and how I can get the value in currentHealth to dynamically update the bar.
Super_Hippo Super_Hippo

2017/1/30

#
Why do you have two variables which *should* do the same? 'curVal' is the value which is displayed by the health bar and 'currentHealth' is not doing much, but the name suggests that it should do the same. 'currentHealth' is not changed anywhere, so it is 100. I am not sure what 'currentHealth' should do, but if it should have any effect, it probably shouldn't be static. (Btw, the 'currentHealth' you use in the 'updateBar' method is a different one although it wouldn't matter if not.)
Theangryman Theangryman

2017/1/30

#
@Super_Hippo Because I'm being smart with resources and using one class for multiple UI elements as I need more than one progress bar, curVar should be where the value in currentHealth is passed through to update this particular instance of ProgBar. currentHealth is the value I want to be able to write to and manipulate when the game is actually running. The value currently in currentHealth is there for testing purposes.
danpost danpost

2017/1/30

#
First, I will presume that there is only the one subclass of CellStats and only one instance of that class is active at any time (or, if there are more than one subclass, that only one instance of any and all of those classes is active at any one time). Line 44, which is supposed to decrease the bar value when the faulty condition is true does not have a counterpart to increase the value when a condition is true.
Super_Hippo Super_Hippo

2017/1/30

#
You mean that you just want to use 'CellStats.currentHealth = 80' to decrease the health to 80 (for example)? That's not really "being smart with ressources" especially not when seeing the fact that you update the health bar image every act cycle even if the value did not change. Anyways (if the answer to the question in sentence 1 is true), I think that in line 44, the first > should be a <. You can remove line 43.
danpost danpost

2017/1/30

#
@Super_Hippo, I believe that the idea with the value is to gradually increase or decrease to the actual value of 'currentHealth' and 'curValue' is the current bar value which may or may not have not reached the actual 'currentHealth' value (like how the Counter class provided by Greenfoot behaves).
Super_Hippo Super_Hippo

2017/1/30

#
Yes I understood that. currentHealth is set to 80 (again), curVal is 100 → the first condition in line 44 is not met. If currentHealth is set to something above 100, the condition is met and curVal decreases. And that's why the condition should be changed.
danpost danpost

2017/1/30

#
Super_Hippo wrote...
Yes I understood that. currentHealth is set to 80 (again), curVal is 100 → the first condition in line 44 is not met. If currentHealth is set to something above 100, the condition is met and curVal decreases. And that's why the condition should be changed.
That I knew -- and referenced in my first post with "faulty condition"; but, I was not referring to that in my last post at all. You seemed to be confused about the two different fields and I was just clarifying them.
Super_Hippo Super_Hippo

2017/1/30

#
Super_Hippo wrote...
You mean that you just want to use 'CellStats.currentHealth = 80' to decrease the health to 80 (for example)?
That was my reply to my first question "why is this currentHealth static?
That's not really "being smart with ressources" especially not when seeing the fact that you update the health bar image every act cycle even if the value did not change.
Means like "even if you have two values, you only have to update the image if they are not the same" and was referring to the resource argument. At this point, I did realize that the two values are used for that updating thing (which I did not see in my first reply). Maybe I should have said that explicitly. I hope it is clear now.
danpost danpost

2017/1/30

#
Super_Hippo wrote...
... Maybe I should have said that explicitly. I hope it is clear now.
Crystal. Keep them going this week. I will be busy for most of it.
Theangryman Theangryman

2017/1/31

#
Sorry I couldn't reply in a timely manor, but thanks for the input guys, I managed to get things working the way I wanted them too.
You need to login to post a reply.