LibGDX HealthBar:我该如何改进它

时间:2014-09-10 03:51:16

标签: java android libgdx

我一直在使用LibGDX做一个简单的游戏,现在我想在我的游戏中添加一个健康栏。 我已经在游戏中成功添加了健康栏,此时此功能正常。

然而,问题是如何像专业人士那样进一步修改此代码。 我还在学习过程中,所以我一直试图想出如何进一步使它看起来 更好,更简洁,更专业,但无法进一步提供。

我已粘贴下面的HealthBar类代码。我将不胜感激任何建议,以便我在将来编写任何代码时能够更好,更有效地思考。

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

2 个答案:

答案 0 :(得分:2)

ShapeRenderer通常仅用于debuging。在最后阶段,大多数时候只使用SpriteBatchdraw方法中,您拨打batch.end(),呼叫flush()。这个方法有点“沉重”,只有在必要时才应该调用。因此,在您的情况下,最好使用batch代替ShapeRenderer来绘制整个HealthBar。 更好的是:
使用Scene2D Progressbar,这应该是您正在寻找的 此ProgressBar需要range(例如min = 0,max = 100),stepSize(例如1.0)boolean verticalProgressBarStyle
您的案例中的ProgressBarStyle只需要2 Drawable s:
backgroung
disabledBackground

然后,您可以使用setValue(maxHealthX),将background设置为0 - maxHealthX(四舍五入到最接近的stepSize)和disabledBackground来自{{ 1}} - 进度条结束。

答案 1 :(得分:1)

这就是你如何做到这一点,实现你自己的逻辑

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