现在我正在开发一个游戏,我正在实现一个健康栏。我在制定正确的公式时遇到了困难。这是我在图片中看到的 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);
}
}
在 50 生命值时,它看起来像这样:
在 75 生命值时,它看起来像这样:
我究竟做错了什么?