Game Development Reference
In-Depth Information
function
AnimatedSprite
.ChangeAnim(
int
num
)
animNum
=
num
// The active animation is now at frame 0 and
0.0f time
frameNum = 0
animTime
= 0.0f
// Set active image, which is just the starting
frame.
int
imageNum
=
animData.frameInfo
[
anim-
Num
].
startFrame
image
=
animData.images
[
imageNum
]
end
The
UpdateAnim
function is where most of the heavy lifting of
AnimatedS-
prite
occurs. Much of its complexity stems from the fact that we can't assume
that the animation frame rate is slower than the game frame rate. For example, a
game could be running at 30 FPS but we may want to play a 24 FPS animation
at 2× speed (or 48 FPS). This means that
UpdateAnim
will quite often need to
skip ahead multiple frames in the animation. To take this into account, once a par-
ticular frame has been displayed longer than the animation frame time, we need to
calculate how many frames to jump ahead. This also means that when a looping
animation wraps around, we can't just reset it to frame 0. It might be that the an-
imation is playing so quickly that we need to reset to frame 1 or 2.
function
AnimatedSprite
.UpdateAnim(
float
deltaTime
)
// Update how long the current frame has been
displayed
frameTime
+=
deltaTime
// This check determines if it's time to change
to the next frame.
if
frameTime
> (1 /
animFPS
)
// The number of frames to increment is the
integral result of
// frameTime / (1 / animFPS), which is