When a spell is on cooldown I display a "unfilling" black bar over the spell GUI until the cooldown is reached. Problem is when i have more than one second of cooldown the scale of the "unfilling" black bar getting strange outcome. I have found the calculation from this website https://gamedevelopment.tutsplus.com/tutorials/how-to-code-hud-cooldown-bars--cms-20983 at the article Skill Buttons Cooldown

float cooldownTime = 1000.0f; //milliseconds.
long elapsedTime = System.currentTimeMillis(); //This is called every second in the main loop
float cooldownEndTime = elapsedTime + cooldownTime;
guiTexture.scale.y = 0.06f; //The "unfilling" black bar over the sprite. 0.06 = cover the whole spell GUI
//When i try to change the scale of the "unfilling" black bar i use this code.
scale.y = (0.06f*(elapsedTime/1000)) - (((0.06f*(elapsedTime/1000))/cooldownEndTime) * elapsedTime);

I have tried to modify the calculation but I havent succeed. If anyone have tackle this problem before or can help me reache a soultion i would be grateful. Thanks


Try this:

elapsedTime (current Java time always) over cooldownEndTime (predicted 1000ms in future) multiplied by maximum bar height 0.06f;

That is scale.y = (elapsedTime/cooldownEndTime)*0.06f; giving you a fraction scaled by a bar height.

Additionally *- dividing elapsed time by 1000 or any fixed number is always going to give an unusual result, since System.currentTimeMillis() represents a number value of milliseconds since about 40 years ago today.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.