Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (746)
Games in Android Showcase (226)
games submitted by our members
Games in WIP (827)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Libgdx] Using progress bar as health bar  (Read 248 times)
0 Members and 1 Guest are viewing this topic.
Offline printer

Junior Devvie





« Posted 2018-01-12 13:50:29 »

Hello, i got a problem. How can i fill in the progress bar with color? Since i would like to use it as a health bar. Also be possible to change color at any time.

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  
public class playerHud implements Disposable {
    private Stage stage;
    private Viewport viewport;

    private Label name;

    private ProgressBar.ProgressBarStyle healthBarStyle;
    private ProgressBar healthBar;

    public playerHud(Main game, Player player) {
        viewport = new ExtendViewport(Main.V_SCREEN_WIDTH, Main.V_SCREEN_HEIGHT, new OrthographicCamera());
        stage = new Stage(viewport, game.batch);
        name = new Label(player.getName(), game.asset.getSkin());
        name.setPosition((Main.V_SCREEN_WIDTH / 2) + player.getName().length() + 15, (Main.V_SCREEN_HEIGHT / 2) + player.getHeight() + 20);

        healthBarStyle = new ProgressBar.ProgressBarStyle();
        healthBarStyle.background = game.asset.getSkin().getDrawable("default-slider");

        healthBar = new ProgressBar(0, 100, 1f, false, healthBarStyle);
        healthBar.setSize(50, 50);
        healthBar.setValue(50);

        stage.addActor(name);
        stage.addActor(healthBar);
    }

    public Stage getStage() {
        return stage;
    }

    @Override
    public void dispose() {
        stage.dispose();
    }
}
Offline buddyBro
« Reply #1 - Posted 2018-01-12 16:17:00 »

I assume you have a colors vbo for your health bar. Then all you have to do is:

1) Give progressBar a private method field float[] color and a setter for that color field.

2) Call the setter when human health changes.

3) When you call healthBar.draw or whatever u call each frame to get it on screen, copy the float array to a float buffer (don't create a new buffer each time, reuse the same one), and then do what you usually do when drawing updated vertex information
e.g.
glBindBuffer(GL_ARRAY_BUFFER, colorsVboId);
glBufferData(GL_ARRAY_BUFFER, colorsBuffer, GL_STATIC_DRAW);

Side note, if the health color is something that doesn't change often, and was something that you used a lot (say on enemy's as well), then it may be worth having a dirty flag, set to true every time color changes, and then you'd only need to update the VBO if the flag were true.

Alternatively, you could do the vbo update in step 2, rather than on frame refresh. I personally wouldn't like mixing "game logic" and "draw logic" into the same method though, especially if u were to later attempt to have separate threads for them.

Another side note, maybe just me, but I wouldn't name the healthBar class ProgressBar, as health doesn't "progress" (it moves in both directions usually). Maybe Bar?
Offline LiquidNitrogen
« Reply #2 - Posted 2018-01-12 17:32:30 »

Since your health bar is an actor, you can use healthBar.setColor(). You probably want to give it a white image so that the colour tint works as expected.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline printer

Junior Devvie





« Reply #3 - Posted 2018-01-12 19:02:40 »

It should be a easier solution that using VBO?

About healthBar.setColor, problem with that it makes the whole screen to the color and not just the health bar :/
Offline LiquidNitrogen
« Reply #4 - Posted 2018-01-13 00:38:23 »

About healthBar.setColor, problem with that it makes the whole screen to the color and not just the health bar :/

I would suspect that either the health bar's background, or perhaps the container/table that its in is filling the whole screen. Or the sprite batch color isnt being reset to (1,1,1,1) for some reason before other things are drawn. setting color that way is definitely going to be the easiest solution if you can work out what is causing that problem.
Offline printer

Junior Devvie





« Reply #5 - Posted 2018-01-13 13:01:37 »

I will try to figure it out.
Pages: [1]
  ignore  |  Print  
 
 

 
xxMrPHDxx (78 views)
2017-12-31 17:17:51

xxMrPHDxx (39 views)
2017-12-31 17:15:51

xxMrPHDxx (45 views)
2017-12-28 18:11:33

Ecumene (215 views)
2017-09-30 02:57:34

theagentd (313 views)
2017-09-26 18:23:31

cybrmynd (362 views)
2017-08-02 12:28:51

cybrmynd (401 views)
2017-08-02 12:19:43

cybrmynd (371 views)
2017-08-02 12:18:09

Sralse (336 views)
2017-07-25 17:13:48

Archive (1089 views)
2017-04-27 17:45:51
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!