现在我正在开发一款游戏而且我正在实施一个健康栏。我坚持做出正确的配方。这是我在图片中看到的Hud代码:
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);
}
}
健康状况看起来像这样:
75健康看起来像这样:
我做错了什么?
答案 0 :(得分:2)
是不是
Resources.healthBar.draw(xHealth, (float) (healthBarHeight / multiplier) - yHealth, healthBarWidth, (float) (healthBarHeight / multiplier));
而不是+ yHealth
?
注意:我不熟悉你正在使用的方法,但看起来它正在使用窗口坐标(从左上角开始),这意味着从开始起的y坐标应该随着健康的增加而减少。
答案 1 :(得分:1)
您对酒吧高度的计算是向后的。我不知道Resource.HealthBar中的参数是什么顺序或它们允许的是什么,但是这里有一些伪代码可以用于你想要的。
topLeft = (healthBarLeft, healthBarTop + (1 - healthPercent) * healthBarHeight)
bottomRight = (healthBarLeft + healthBarWidth, healthBarTop + healthBarHeight)
(1 - healthPercent)意味着当你剩下10%的生命值时,该栏会在90%的位置开始。
编辑:如果它不支持topLeft / bottomRight而是需要宽度/高度,请使用:
dimensions = (healthBarWidth, healthPercent * healthBarHeight)