Game Development Reference
In-Depth Information
Next, we will move this texture rectangle once in a while to simulate a rotating crystal. In
the previous code, we set the number of frames to eight (as many as there are in the sprite
sheet), and set the time of the animation to one second in total, which means that each
frame stays for about 0.125 seconds (the animation duration is divided by the number of
frames) at a time. We know what we want to do now, so let's do it:
In the code, we first measure the delta time since the last frame and add it to the accumu-
lated time. The last two lines of the code actually do all the work. The first one looks in-
timidating at first glance, but it is simply a way to choose the correct frame, based on how
much time has passed and how long the animation is. The formula timeAsSeconds /
animationDuration gives us the time relative to the animation duration. So let's say
that 0.4 seconds have passed and our animation duration is 1 second. This leaves us with
0.4 seconds in local animation time. Multiply this 0.4 seconds by the number of frames,
and we get the following result:
0.4 * 8 = 3.2
This gives us which frame we should be on at the moment, and how long we have been
there. The current frame index is the whole part of 3.2 (which is three), and the fraction
part (0.2) is how long we have been on that frame. In this case, we are only interested in
the current frame so we will take that by casting the whole expression to int . This
rounds the number down if the number is positive (which it always is in this case). The
Search WWH ::




Custom Search