If you've ever played a 2D video game, you know that we are still missing one vital component: sprite animation. The animation consists of so-called keyframes, which produce the illusion of movement. Figure 8-25 shows a nice animated sprite by Ari Feldmann (part of his royalty-free SpriteLib).
The image is 256x64 pixels in size, and each keyframe is 64x64 pixels. To produce animation, we just draw a sprite using the first keyframe for some amount of time—say, 0.25 seconds—then we switch to the next keyframe, and so on. When we reach the last frame we have two options: we can stay at the last keyframe or start at the beginning again (and perform what is called a looping animation).
We can easily do this with our TextureRegion and SpriteBatcher classes. Usually we'd not only have a single animation like in Figure 8-25, but many more in a single atlas. Besides the walk animation, we could have a jump animation, an attack animation, and so on. For each animation we need to know the frame duration, which tells us how long we keep using a single keyframe of the animation before we switch to the next frame.
Was this article helpful?