- Home /
Javascript Maths
Hi, I am trying to solve this problem which I thought would be simple. I have written a script to control the size of a health bar for the enemies in my game, however the maths in java don't seem to be working correctly. Can anyone help?
var StartingHealth:int = 10;
Health = (Health-damage);
var HealthPercentage = Health/StartingHealth;
HealthBar.pixelInset.width = HealthBarBorder*HealthPercentage;
The plan was to find the percentage of the health in comparison to the starting health and adjust the health bar size with that. However when I try it always retuns 0.
E.g.
var HealthPercentage = 9/10; (= 0.9)
HealthBar.pixelInset.width = 100*0.9 (= 0? - Should be 90)
Sorry if it is confusing. Thanks
Answer by IgorAherne · Aug 13, 2013 at 09:25 PM
I've tried that code with a new script:
var StartingHealth:float = 10;
var Health : float = 100;
var damage : float = 10;
var HealthPrecentage : float;
var result : float;
var HealthBarBorder = 20.2;
Health = (Health-damage);
var HealthPercentage = Health/StartingHealth;
result = HealthBarBorder*HealthPercentage;
function Start(){
Debug.Log("result is " + result);
}
everything is working. Perhaps, some of your variables are of different types, that are incompatible when dividing or multiplying ( int with float for example) Either that, or it's HealthBar's problem
Well I did pretty much something similar to this, I used a float to get the percentage decimal then multiplied it by the bar width, but it always returns 0. Thanks for the reply, Ill try your method.
Your answer
