Game Development Reference
In-Depth Information
frameTime * animFPS
frameNum
+=
frameTime
*
animFPS
// Check if we've advanced past the last
frame, and must wrap.
if
frameNum
>=
animData.frameInfo
[
anim-
Num
].
numFrames
// The modulus (%) makes sure we wrap
correctly.
// (Eg. If numFrames == 10 and frameNum
== 11, frameNum would
// wrap to 11 % 10 = 1).
frameNum
=
frameNum
%
anim-
Data.frameInfo
[
animNum
].
numFrames
end
// Update the active image.
// (startFrame is relative to all the im-
ages, while frameNum is
// relative to the first frame of this par-
ticular animation).
int
imageNum
=
animData.frameInfo
[
anim-
Num
].
startFrame
+
frameNum
image
=
animData.images
[
imageNum
]
// We use fmod (floating point modulus) for
the same reason
// as the % above.
frameTime
= fmod(
frameTime
, 1 /
animFPS
)
end
end
Althoughthisimplementationof
AnimatedSprite
willworkforloopinganim-
ations, it has no support for transitions between animations. If such functionality is
required, it's advisable to create an animation state machine.
Chapter 9
,
“
Artificial
Intelligence
,
” discusses the state design pattern in the context of AI, but said state
pattern could just as easily apply to an animation state machine.