现在我正在开发一款游戏,我正在实现一个健康条。我在做正确的配方时遇到了困难。这是我在图片中看到的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健康状态下,它看起来像这样:

我做错了什么?
发布于 2013-08-27 22:13:32
会不会是
Resources.healthBar.draw(xHealth, (float) (healthBarHeight / multiplier) - yHealth, healthBarWidth, (float) (healthBarHeight / multiplier));代替+ yHealth
注意:我不熟悉您正在使用的方法,但它似乎使用了窗口坐标(从左上角开始),这意味着开始时的y坐标应该随着健康的增加而减少。
发布于 2013-08-27 22:12:50
你对横杆高度的计算是反向的。我不知道Resource.HealthBar中的参数是什么顺序,也不知道它们允许什么,但这里有一些您想要的伪代码。
topLeft = (healthBarLeft, healthBarTop + (1 - healthPercent) * healthBarHeight)
bottomRight = (healthBarLeft + healthBarWidth, healthBarTop + healthBarHeight)(1 - healthPercent)意味着当你还剩下10%的生命值时,条形将从90%开始下降。
编辑:如果它不支持topLeft / bottomRight,而是需要一个宽/高,请使用:
dimensions = (healthBarWidth, healthPercent * healthBarHeight)https://stackoverflow.com/questions/18467670
复制相似问题