Game Development Reference
In-Depth Information
Not bad for three lines of code!
The width of the iapan object is decreased by 1 each frame, so if its width is 50 pixels, it will take
50 frames for the meter to reach zero. If you made a really long meter, it might take a bit of time
before it runs out. You can easily speed up or slow down the rate at which the meter decreases by
changing this line:
For example, if you change the line like this, it reduces the meter by 2 pixels each frame:
This doubles the speed by which the health meter is reduced. (The complete code is available in the
I]ej[Lh]ucnkqj`[0*]o file in the chapter's source files.)
Using scaleX to scale the meter based on a percentage
You can also use the o_]haT property to change the meter's size. o_]haT changes the size of an object
based on its ratio. You can use it to change the width of the meter based on a percentage instead of
a fixed pixel amount. This is often preferable because it means the meter is reduced at the same rate,
no matter how long or short it is.
A o_]haT value of 1 means the object is full size. You can reduce a meter by 1% by subtracting 0.01
from the o_]haT property each frame.
To duplicate the effect of this example using o_]haT, you first need to add an additional eb statement
to make sure that the o_]haT value is above zero. Without this check, the meter continues to scale
negatively beyond the left border, which would not look very good. Here's the code to use:
A value of ,*,. reduces the meter by 2% each frame, so the meter will reach 0 in 50 frames. To check
for the end of the game using o_]haT, use this code:
Scaling by percentages is very useful because you can use meters to graph other data that might be
using percentages in your game.
Search Nedrilad ::

Custom Search