首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >垂直健康条

垂直健康条
EN

Stack Overflow用户
提问于 2013-08-27 22:04:10
回答 2查看 440关注 0票数 2

现在我正在开发一款游戏,我正在实现一个健康条。我在做正确的配方时遇到了困难。这是我在图片中看到的Hud的代码:

代码语言:javascript
复制
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健康状态下,它看起来像这样:

我做错了什么?

EN

回答 2

Stack Overflow用户

发布于 2013-08-27 22:13:32

会不会是

代码语言:javascript
复制
Resources.healthBar.draw(xHealth, (float) (healthBarHeight / multiplier) - yHealth, healthBarWidth, (float) (healthBarHeight / multiplier));

代替+ yHealth

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

票数 2
EN

Stack Overflow用户

发布于 2013-08-27 22:12:50

你对横杆高度的计算是反向的。我不知道Resource.HealthBar中的参数是什么顺序,也不知道它们允许什么,但这里有一些您想要的伪代码。

代码语言:javascript
复制
topLeft = (healthBarLeft, healthBarTop + (1 - healthPercent) * healthBarHeight)
bottomRight = (healthBarLeft + healthBarWidth, healthBarTop + healthBarHeight)

(1 - healthPercent)意味着当你还剩下10%的生命值时,条形将从90%开始下降。

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

代码语言:javascript
复制
dimensions = (healthBarWidth, healthPercent * healthBarHeight)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18467670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档