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

2014/4/16

้How to lose health from the left side on Healthbar

Maydonna Maydonna

2014/4/16

#
Hello, I've made a HealthCounter class for making hp bar and I have two players in my game. My first player's hp bar was fine with decreasing health from the right side but I want the second player's hp bar to decrease from the left side. How can I make it ? Here is my code for making hp bar. For your information Thank you (:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class HealthCounter extends Actor
{
    int health = 180;
    int healthBarWidth = 180;
    int healthBarHeight = 16;
    GreenfootImage hp;
 
    public void act()
    {
        update();
    }
 
    public void update(){
        setImage(new GreenfootImage(healthBarWidth + 2,healthBarHeight + 2));
        hp = getImage();
        hp.setColor(Color.WHITE);
        hp.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1);
        //when health is less than 80, become red.
        if(health < 80){
            hp.setColor(Color.RED);
        } else{ //the default color is green
            hp.setColor(Color.GREEN);
        }
         
        hp.fillRect(1, 1, health, healthBarHeight);
    }
 
    public void loseHealth(){
        health = health - 10;
    }
}
Super_Hippo Super_Hippo

2014/4/16

#
I guess you have to increase the first number in line 25.
Maydonna Maydonna

2014/4/16

#
Super_Hippo wrote...
I guess you have to increase the first number in line 25.
OK, how and why ? btw, thank you for your answer (:
danpost danpost

2014/4/16

#
Super_Hippo wrote...
I guess you have to increase the first number in line 25.
There is a little more to it than that. Any simple changes like this will change all HeathCounter objects created. So, the healthbar for the first player will then be backward also. What is needed is a way for the HealthCounter class to know which draw method to use; the one that was originally given or the modified one. This can be done by passing a value to the class when creating a HealthCounter object. It could be an int (the number of the player) or a boolean (for reversedDirection). The boolean would be easy to work with.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class HealthCounter extends Actor
{
    int health = 180;
    int healthBarWidth = 180;
    int healthBarHeight = 16;
    GreenfootImage hp;
    boolean reversed; // added this field
 
    // added constructor w/o Boolean
    public HealthCounter()
    {
        this(false); // calls constructor below with false boolean value
    }
 
    // added constructor to receive boolean
    public HealthCounter(boolean backward)
    {
        reversed = backward;
        update();
    }
 
    /**
     * There is no need to set the image every act.
     * It only needs to be updated when the value changes.
     * You can remove the 'act' method
     */
    // public void act()
    // {
    //     update();
    // }
 
    public void update()
    {
        setImage(new GreenfootImage(healthBarWidth + 2,healthBarHeight + 2));
        hp = getImage();
        hp.setColor(Color.WHITE);
        hp.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1);
        //when health is less than 80, become red.
        if(health < 80){
            hp.setColor(Color.RED);
        } else{ //the default color is green
            hp.setColor(Color.GREEN);
        }
        if (!reversed) // added 'if-else'
        {
            hp.fillRect(1, 1, health, healthBarHeight);
        }
        else
        {
            hp.fillRect(healthBarWidth-health+1, 1, health, healthBarHeight);
        }
    }
 
    public void loseHealth()
    {
        health = health - 10;
        update(); // add this call to adjust the image only when needed
    }
}
If you add any other methods that change the value of 'health', call 'update' at the end of each one.
danpost danpost

2014/4/16

#
When you create the HealthCounter for the first player, you can use:
1
2
3
HealthCounter hpCounter1 = new HealthCounter();
// or
HealthCounter hpCounter1 = new HealthCounter(false);
But, when creating one for the second player, you need to use:
1
HealthCounter hpCounter2 = new HealthCounter(true);
Super_Hippo Super_Hippo

2014/4/16

#
Maydonna wrote...
OK, how and why ? btw, thank you for your answer (:
In the API, you can see that these two numbers represent the bottom left corner of the rectangle you want to draw related to the bottom left corner of the image the rectangle will be drawn at(. Or on? I don't know). First the distance in the x directions, second in y. The other two numbers are for the width and height of the rectangle. The "how" answered danpost good enough, I guess. ;)
Maydonna Maydonna

2014/4/16

#
@danpost and @Super_Hippo thank you you guys (: I appreciate it.
danpost danpost

2014/4/16

#
Super_Hippo wrote...
In the API, you can see that these two numbers represent the bottom left corner of the rectangle you want to draw related to the bottom left corner of the image the rectangle will be drawn at(. Or on? I don't know). First the distance in the x directions, second in y. The other two numbers are for the width and height of the rectangle.
Replace all occurrences of the word 'bottom' with the word 'top'.
Super_Hippo Super_Hippo

2014/4/16

#
Ugh, you are right. I should have checked the API before answering.
You need to login to post a reply.