Прямо сейчас я работаю над игрой, и я реализую панель здоровья. Я застрял, делая правильную формулу. Это мой код Худ вы видите на картинке:Vertical Healthbar
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 здоровье это выглядит следующим образом:
Что я делаю неправильно?
Что такое «множитель»? – DerMike
@ Dennisch Разве это не просто повторение проблемы OP? – millimoose
Это помогло бы, если бы у вас были отдельные переменные для ограничивающей рамки панели «контейнер» и панели здоровья. Не совсем понятно, когда вы рисуете что-то, вы должны объяснить, что означают параметры 'draw()'. Кроме того, если 'Player.getHealth()' '' double'? Если вы не используете тот факт, что вы используете интегральное подразделение. – millimoose