Login | Register

Percentage of delay

September 9, 2006, 17:43 by Calle
[loading]
-->
<b>Question:</b> Whenever my character shoots, the variable, 'delay', goes up to a number. Every step it goes down to zero. You can't shoot unless it's at zero. I made a sprite for the delay, with 26 frames, starting on no delay, then ending on full delay. I want the frame to be of the percentage the delay is of 100. For example, if the delay was 25, it would be 1/4th, which would make the sprite's image_index be 6. How would I go upon coding this?

<b>Answer:</b> You seem to have figured out most of it yourself, although now you have to let the computer do the computing. If the delay was 25, and the maximum value is 100, you correctly calculated that it would be one fourth, which corresponds to the sixth image. Maybe you just knew that 25/100 equals 1/4, but it is also very possible that you divided 25 with 100, and got the result 0.25 ... next step is to find out which image, and it as simple as multiplying with 24. Our problem now is that it might not return equal, with 25 it luckily does and therefore 25/100*24 works fine, but what if it doesn't? We will obviously have to round it down, or up. The function for this is actually, very logical, "round(x)". So, the complete calculation will look like this:

image_index = round(delay/100 * 26);

<b>Epilogue: </b> Just some minutes after I had posted my solution, Venomblood kindly responded to it:
Thanks. It works. I changed it to this:
image_single=round(obj_character.delay/obj_character.maxdelay*26)
Since the thing with the image_index isn't the character.

Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games