Java LibGDX HealthBar:我如何改进它
我一直在用LibGDX制作一个简单的游戏,现在我正在尝试在我的游戏中添加一个健康栏。 我已经成功地在游戏中添加了健康栏,目前运行良好 然而,问题是如何像专业人员那样进一步修改此代码。 我仍在学习过程中,所以我一直在试图找出如何进一步使它看起来更像 更好、简洁、专业,但我们无法进一步提供 我已经在下面粘贴了我的HealthBar类代码。我将感谢任何建议,以便我在将来编写任何代码时能够思考得更好、更高效Java LibGDX HealthBar:我如何改进它,java,android,libgdx,Java,Android,Libgdx,我一直在用LibGDX制作一个简单的游戏,现在我正在尝试在我的游戏中添加一个健康栏。 我已经成功地在游戏中添加了健康栏,目前运行良好 然而,问题是如何像专业人员那样进一步修改此代码。 我仍在学习过程中,所以我一直在试图找出如何进一步使它看起来更像 更好、简洁、专业,但我们无法进一步提供 我已经在下面粘贴了我的HealthBar类代码。我将感谢任何建议,以便我在将来编写任何代码时能够思考得更好、更高效 public class HealthBar extends Actor{ protected
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;
}
shaperender
通常仅用于调试。在终局中,大多数情况下只使用了SpriteBatch
。在
draw
方法中调用batch.end()
,该方法调用flush()
。这个方法有点“繁重”,只有在确实必要时才应该调用它。因此,在您的情况下,最好使用batch
而不是shaperender
来绘制整个HealthBar
更好:
使用
Scene2D
Progressbar
,这正是您要查找的内容。此
ProgressBar
需要范围(例如min=0,max=100),步长(例如1.0),布尔垂直和ProgressBarStyle
您的ProgressBarStyle
只需要2个Drawable
s:
背景
disabledBackground
然后,您可以使用setValue(maxHealthX)
,它将背景设置为0-maxHealthX
(四舍五入到最接近的stepSize
)和maxHealthX
进度条末尾的禁用背景设置 shaperender
通常仅用于调试。在终局中,大多数情况下只使用了SpriteBatch
。
在draw
方法中调用batch.end()
,该方法调用flush()
。这个方法有点“繁重”,只有在确实必要时才应该调用它。因此,在您的情况下,最好使用batch
而不是shaperender
来绘制整个HealthBar
更好:
使用Scene2D
Progressbar
,这正是您要查找的内容。
此ProgressBar
需要范围(例如min=0,max=100),步长(例如1.0),布尔垂直和ProgressBarStyle
您的ProgressBarStyle
只需要2个Drawable
s:
背景
disabledBackground
然后,您可以使用setValue(maxHealthX)
,它将背景设置为0-maxHealthX
(四舍五入到最接近的stepSize
)和maxHealthX
进度条末尾的禁用背景设置 这就是你如何做到的,自己实现逻辑
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;
}
}
这就是你如何做到的,你自己实现逻辑
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;
}
}
您好,Springrbua感谢您提供更好的解决方案。:)我将接受你的建议并进一步提供。谢谢@我很高兴能帮上忙!请告诉我它是否有效:)您好,斯普林伯亚,谢谢您提供了更好的解决方案。:)我将接受你的建议并进一步提供。谢谢@我很高兴能帮上忙!请告诉我它是否有效:)