2

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

索2

我究竟做错了什么?

4

2 回答 2

2

可不可能是

Resources.healthBar.draw(xHealth, (float) (healthBarHeight / multiplier) - yHealth, healthBarWidth, (float) (healthBarHeight / multiplier));

而不是+ yHealth

注意:我不熟悉您使用的方法,但似乎它使用的是窗口坐标(从左上角开始),这意味着开始的 y 坐标应该随着健康状况的增加而减少。

于 2013-08-27T14:13:32.133 回答
1

您对条形高度的计算是向后的。我不知道 Resource.HealthBar 中的参数的顺序或它们允许的顺序,但这里有一些你想要的伪代码。

topLeft = (healthBarLeft, healthBarTop + (1 - healthPercent) * healthBarHeight)
bottomRight = (healthBarLeft + healthBarWidth, healthBarTop + healthBarHeight)

(1 - healthPercent) 表示当您还剩 10% 的生命值时,条形图将从 90% 开始向下。

编辑:如果它不支持 topLeft / bottomRight 而需要一个宽度/高度,请使用:

dimensions = (healthBarWidth, healthPercent * healthBarHeight)
于 2013-08-27T14:12:50.307 回答