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

2018/11/25

(Urgent) How do I do a health bar

SuperAmigo300 SuperAmigo300

2018/11/25

#
Hello, I'm trying to create a bar of life with a big amount of health , I followed a tutorial on Youtube, the issue here is that I need "health" to be 4000, but if the value of health is greater than the width of the bar, it doesn't render it (using the same formula) I'll leave here the code.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and Mou seInfo)
 
/**
 * Write a description of class HealthBar here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class HealthBar extends Actor
{
    static int health = 4000;
    int healthBarWidth = 160;
    int healthBarHeight = 3;
    int pixelsPerHealthPoint = (int)healthBarWidth/health;
    /**
     * Act - do whatever the HealthBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     
    public HealthBar(){
        update();
    }
     
     
    public void act()
    {
        update();
         
    }
    public void update(){
        setImage(new GreenfootImage(healthBarWidth, healthBarHeight));
        GreenfootImage healthbarim = getImage();
        healthbarim.setColor(Color.RED);
        healthbarim.fillRect(1, 1, health * pixelsPerHealthPoint, healthBarHeight);
    }
     
    public void loseHealth(){
        health -=1;
    }
     
 
     
}
danpost danpost

2018/11/25

#
SuperAmigo300 wrote...
I'm trying to create a bar of life with a big amount of health , I followed a tutorial on Youtube, the issue here is that I need "health" to be 4000, but if the value of health is greater than the width of the bar, it doesn't render it (using the same formula) I'll leave here the code. << Code Omitted >>]
Your problem is that you do not have pixels per health -- you have health per pixels. That is to say, because you have more health that the number of pixels in your bar, pixels per health would be a fractional amount. This will cause your pixelsPerHealth (int) value to become zero. Use health points per pixel (the reciprocal) and then divide for the bar size.
SuperAmigo300 SuperAmigo300

2018/11/25

#
danpost wrote...
SuperAmigo300 wrote...
I'm trying to create a bar of life with a big amount of health , I followed a tutorial on Youtube, the issue here is that I need "health" to be 4000, but if the value of health is greater than the width of the bar, it doesn't render it (using the same formula) I'll leave here the code. << Code Omitted >>]
Your problem is that you do not have pixels per health -- you have health per pixels. That is to say, because you have more health that the number of pixels in your bar, pixels per health would be a fractional amount. This will cause your pixelsPerHealth (int) value to become zero. Use health points per pixel (the reciprocal) and then divide for the bar size.
Sorry but my english isn't very good and would be better if you explain me that concept with code.
danpost danpost

2018/11/25

#
Change line 14 to:
1
int healthPointsPerPixel = health/healthBarWidth;
and line 34 to:
1
healthbarim.fillRect(0, 0, health / healthPointsPerPixel, healthBarHeight);
SuperAmigo300 SuperAmigo300

2018/11/25

#
So much thanks!!!
but how would you connect that to your charictor
danpost danpost

2018/11/26

#
ebollinger@sonorahigh.net wrote...
but how would you connect that to your charictor
As the HealthBar class is written above, no real connection is needed. That is, any class can access the health value and change it at will. For example:
1
HealthBar.health--;
could be used without a need for getting a reference to a specific instance of a HealthBar object. It is very restrictive, however, as it does not take advantage of the versatility that object-oriented programming has to offer. The main drawback is that any and all health bars created will contain and display the same exact value. Also, unless the value is reset by code elsewhere (like in the initial world constructor), resetting the scenario will not restore the health value. For a tutorial on basic displaying of a value (including health bar values), see my Value Display Tutorial scenario.
k thanks!
But what if you need it fore multiple Actors
But what if you need it fore multiple Actors
But what if you need it fore multiple Actors
danpost danpost

2018/11/28

#
ebollinger@sonorahigh.net wrote...
But what if you need it fore multiple Actors
Then you cannot use the static modifier on the health field. As non-static, each instance will have its own health value.
Or you can make difrent health bars
danpost danpost

2018/11/28

#
ebollinger@sonorahigh.net wrote...
Or you can make difrent health bars
If you mean make multiple health bar classes -- not an idea worth considering. Keep it simple. Have one class to display a bar. Create how ever many bars you need from that one class.
You need to login to post a reply.