Reputation: 231
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;
}
@Override
public void draw(Batch batch, float parentAlpha){
//if a main character is hit by enemy
if(isHitE==true){
//subtractHP();
totalSubractedHP += 1;
isHitE = false;
}
if(isHitI==true){
totalAddedHP += 10;
isHitI = false;
}
MaxHealthX=296-decreaseRate+totalAddedHP-totalSubractedHP;
batch.end();
if(!projectionMatrixSet){
sr.setProjectionMatrix(batch.getProjectionMatrix());
}
sr.begin(ShapeType.Filled);
sr.setColor(Color.BLACK);
sr.rect(40,650,300,40);
if(isOutOfHealth() == false){
sr.setColor(Color.RED);
sr.rect(42, 652, MaxHealthX, HealthBarY);
}
sr.end();
batch.begin();
}
//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;
}else{
return true;
}
}
//checking if hit by enemy
public boolean isHitByEnemy(){
isHitE = true;
return isHitE;
}
public boolean isHitByItem(){
isHitI = true;
return isHitI;
}
Upvotes: 0
Views: 2665
Reputation: 1571
This is how you can do it ,implement logic your self
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
int i=0;
Texture texture,texture2;
@Override
public void create () {
batch = new SpriteBatch();
initTestObjects() ;
}
@Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(texture2,100,100,300,20);
batch.draw(texture,100,100,i,20);
if(i>300)
{
i=0;
}
i++;
batch.end();
}
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);
pixmap.fill();
return pixmap;
}
}
Upvotes: 2
Reputation: 9793
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 Drawable
s:
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.
Upvotes: 3