LibGDX HealthBar: How can i improve it

I have been making a simple game using LibGDX and now I am trying to add a health bar in my game. I have successfully added the health bar in the game and it is working fine at this moment.

The problem , however, is how to further modify this code like professionals do. I am still in learning process so I have been trying to figure how to further make it look better, succinct, and professional but wan't able to furnish it further.

I have pasted my HealthBar class code below. I will appreciate any suggestion so that I can think better and efficient when writing any codes in future.

public class HealthBar extends Actor{

protected float MaxHealthX;
protected static float HealthBarY= 36;
float decreaseRate = 0;
float addHealth = 0;
float deductHealth = 0;
ShapeRenderer sr;
static private boolean projectionMatrixSet;
private boolean isHitE = false;
private boolean isHitI = false;
float totalSubractedHP = 0;
float totalAddedHP = 0;

public HealthBar(){

    sr = new ShapeRenderer();
    projectionMatrixSet = false;


public void draw(Batch batch, float parentAlpha){

    //if a main character is hit by enemy
        totalSubractedHP += 1;
        isHitE = false;

        totalAddedHP += 10;
        isHitI = false;


    if(isOutOfHealth() == false){
    sr.rect(42, 652, MaxHealthX, HealthBarY);




//updating health decrease rate as time passes by
public void updateHealthBar(float delta) {
    decreaseRate += 10*delta;       


//checks whether health is greater than zero
public boolean isOutOfHealth(){
    if(MaxHealthX > 0){
        return false;
        return true;

//checking if hit by enemy
public boolean isHitByEnemy(){
    isHitE = true;
    return isHitE;

public boolean isHitByItem(){
    isHitI = true;
    return isHitI;


ShapeRenderer is normaly only used for debuging. In the endgame most times only SpriteBatch is used. In the draw method you call batch.end() which calls flush(). This method is a bit "heavy" and should only be called, if it is really neccessary. So in your case it would be better to use batch instead of ShapeRenderer to draw the whole HealthBar. Even better: Use the Scene2D Progressbar, which should be exactly what you are looking for. This ProgressBar need a range (for example min = 0, max = 100), a stepSize (for example 1.0) a boolean vertical, and a ProgressBarStyle. The ProgressBarStyle in your case just needs 2 Drawables: backgroung disabledBackground

You can then use setValue(maxHealthX), which will set the background from 0-maxHealthX (rounded to the nearest stepSize) and disabledBackground from maxHealthX-end of progressbar.

This is how you can do it ,implement logic your self

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;

public class MyGdxGame extends ApplicationAdapter {
    SpriteBatch batch;
     int i=0;
    Texture texture,texture2;
    public void create () {
        batch = new SpriteBatch();

        initTestObjects() ;

    public void render () {, 0, 0, 0);;


    private void initTestObjects() {

        int width =1 ;
        int height = 1;
        Pixmap pixmap = createProceduralPixmap(width, height,0,1,0);
        Pixmap pixmap2 = createProceduralPixmap(width, height,1,0,0);

    texture = new Texture(pixmap);
    texture2 = new Texture(pixmap2);


    private Pixmap createProceduralPixmap (int width, int height,int r,int g,int b) {
        Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888);

        pixmap.setColor(r, g, b, 1);

        return pixmap;

Need Your Help

Create ejabberd user from PHP

php xmpp ejabberd

I need to create an ejabberd user from a PHP script. I also need to be able to add the new user to a predefined shared roster.

C++ fstream error unknown

c++ fstream

I'm trying to make a wrapper for a file - so a small wrapper to fstream. I am in the process of making something that will want to read/write binary and text to file, so I can make model loaders ta...