문제

So I am creating a battle system for my own personal enjoyment and am trying to tackle the healing portion of it. When I use a healing item it increases the max health, when the value exceeds the current max health, to whatever the value of the healing affect was.

I looked it up and some people suggested Saturation Arithmetic, but I couldn't find anything on google that explained that process. Then I found another topic on Stack that explained the same problem and had an answer, however when I tried it it's not working. Long story short.

Here is the block of code I'm working with currently.

public void useHealItem2(CharStats hero, Enemy enemy, Battle battle){
    Math.min(hero.hp += 500, hero.hp);
    battle.herodmg = enemy.att - hero.def;
    hero.hp -= battle.herodmg;
}

So I am trying to set the max to a variable amount rather then a fixed number. So as the hero's hp increases this will automatically scale. I think this is having problems because it's setting the hp first so the maximum that it's going off of is already at 500. If that makes sense. But that's just what I'm thinking, I really don't know for sure.

도움이 되었습니까?

해결책

I would suggest saying hero.hp = Math.min(hero.currentHealth+500, hero.maxHealth), where currentHealth and maxHealth are exactly what they sound like.

다른 팁

As @Stendika has answered, using Math.min(hero.hp + 500, hero.maxHealth) would be the solution, it then takes the minimum of both, and if hero.hp + 500 > hero.maxHealth it will take hero.maxHealth as requested.
(Where we have made up a variable hero.maxHealth that stores the maximum health of that hero, which is a must if you want to calculate it in your suggested way)

However it is also very important that you understand what is happening right now, to explain:

  • You are using Math.min(hero.hp += 500, hero.hp)
  • You assign the returned value of the above statement to no variable, hence it appears to do nothing.
  • hero.hp += 500 still gets evaluated and hence executed, what is does is add 500 to hero.hp, hence hero.hp has now been changed.
  • Because of this, it has 'bypassed' your calculations and appears to behave as if you would have added simply 500 to hero.hp.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top