Java 垂直健康栏
现在我正在做一个游戏,我正在实现一个健康酒吧。我一直在做正确的公式。这是我在图中看到的Hud代码:Java 垂直健康栏,java,slick2d,Java,Slick2d,现在我正在做一个游戏,我正在实现一个健康酒吧。我一直在做正确的公式。这是我在图中看到的Hud代码: public class CharacterHUD { private Image image; private Player player; private float xHealth, yHealth; private float healthBarWidth, healthBarHeight; private double healthBarY =
public class CharacterHUD {
private Image image;
private Player player;
private float xHealth, yHealth;
private float healthBarWidth, healthBarHeight;
private double healthBarY = 0;
private double multiplier;
public CharacterHUD(float xHealth, float yHealth, Image image, Player player) {
this.image = image;
this.player = player;
this.xHealth = xHealth; // X Offset from the frame, 20 in this case.
this.yHealth = yHealth; // Y Offset from the frame, 17 in this case.
healthBarWidth = 38;
healthBarHeight = 173;
}
public void update(int delta) {
healthBarY = (yHealth / 100 * player.getHealth());
multiplier = (yHealth / healthBarY);
Log.log("Percentage: " + multiplier);
Log.log("yHealth: " + yHealth + "+ Health: " + healthBarY);
Log.log("Actual yHealth: " + (healthBarHeight * multiplier));
}
public void render(Graphics g) {
// The coordinates are going from top-left(x,y) to bottomright(width,height). Also, the yHealth is the offset from the frame.
Resources.healthBar.draw(xHealth, (float) (healthBarHeight / multiplier) + yHealth, healthBarWidth, (float) (healthBarHeight / multiplier));
image.draw(0, 0);
}
}
在50 health上,它看起来是这样的:
关于75 health,它看起来是这样的:
我做错了什么?你对横杆高度的计算是反向的。我不知道Resource.HealthBar中的参数的顺序是什么,或者它们允许什么,但是这里有一些您想要的伪代码
topLeft = (healthBarLeft, healthBarTop + (1 - healthPercent) * healthBarHeight)
bottomRight = (healthBarLeft + healthBarWidth, healthBarTop + healthBarHeight)
(1-healthPercent)意味着当你剩下10%的生命值时,酒吧将开始90%的下降
编辑:如果它不支持左上角/右下角,而是需要宽度/高度,请使用:
dimensions = (healthBarWidth, healthPercent * healthBarHeight)
是吗
Resources.healthBar.draw(xHealth, (float) (healthBarHeight / multiplier) - yHealth, healthBarWidth, (float) (healthBarHeight / multiplier));
而不是+yHealth
注意:我不熟悉您使用的方法,但它似乎使用了窗口坐标(从左上角)这意味着开始时的y坐标应该随着健康度的增加而减少。乘法器应该做什么?@Dennisch这不只是重复OP的问题吗?如果健康栏“容器”的边界框和健康栏本身有单独的变量,这会有所帮助。当你在画什么的时候并不十分清楚,你应该解释
draw()
的参数是什么意思。另外,如果Player.getHealth()
adouble
?如果不是的话,使用整数除法可能会有帮助。另外,它不应该是healthBarHeight*乘法器?看起来,乘数
将是一个介于0
和1
之间的数字,所以事实上,你应该乘以最大高度,以得到你应该填充的高度。好吧,也许我的回答有点过于直截了当:)这是水平healthbar的方法。但是,这是一个垂直坐标,当健康度变低时,X坐标应该变低。@TimondeGroot嗯。你确定是X(即“左”/“宽”)坐标应该降低吗?哦,我一直在搞砸X和y:p。我的意思是Y。坐标是从左上角(x,Y)到右下角(宽度,高度)。此外,Y健康是与帧的偏移量。@TimondeGroot此类信息属于问题。即在draw()参数旁边的注释中