[Libgdx] Using progress bar as health bar

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.

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();
    }
}

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?

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.

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 :confused:

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.

I will try to figure it out.