Designing an Animated Sprite


Even though sprites are already animated in the sense that they can move around on the game screen, for the purposes of this discussion I'm going to refer to a frame-animated sprite as simply an animated sprite . The animation, in this case, means that the sprite's appearance is being animated. The first place to start in designing an animated sprite is its bitmap image.

You learned in the previous section how a series of images is used to represent an animated sprite because the images can be cycled through to give the effect of animation. There are a variety of different ways to store a series of images for an animated sprite, but the easiest I've found is to store the frame images horizontally in a single image. For example, the series of Pac-Man images you saw in Figure 17.1 shows several frame images appearing next to each other. These four frame images could be stored together just as they appear in the figure as a single image. It then becomes the responsibility of the animated sprite to draw only the frame image representing the current frame. You can think of the sprite image at any given moment as being a little window that moves from one frame image to the next to reveal only one frame image at a time. Figure 17.2 shows what I'm talking about.

Figure 17.2. The current sprite image is drawn from within the series of frame images.

graphics/17fig02.gif

This figure shows how the second frame of the Pac-Man sprite is currently being displayed. The figure also reveals how the bitmap image for an animated sprite is put together. The image is the same height as a normal unanimated sprite image, but its width is determined by the number of frames. So, if a single frame is 25 pixels wide and there are a total of four frames of animation, the entire image is 100 pixels wide. To create an image like this, you just place the frame images immediately next to each other with no space in between them.

With the animated sprite image in place, you can now turn your attention to the actual data for the sprite that is used to manage frame animation. First of all, you know that the sprite needs to understand how many frame images there are because this is crucial in determining how to cycle through the frames of the sprite's bitmap image. In addition to knowing the total number of animation frames, it's also necessary to keep track of the currently selected frame ”this is the frame image currently being displayed.

Earlier in the hour , I talked about how it's important to be able to control the speed at which an animated sprite cycles through its frame images. This speed is controlled by a piece of information known as a frame delay . The frame delay for a sprite basically determines how many game cycles must elapse before the frame image is changed. In order to carry out the frame delay feature for an animated sprite, you must keep up with a trigger (counter) that counts down the delay and indicates that it's time to move to the next frame. So, when an animated sprite first starts out, the trigger is set to the frame delay, and it begins counting down with each game cycle. When the trigger reaches zero, the sprite moves to the next frame and resets the trigger to the frame delay again.

To recap, the following pieces of information are required of the new animated sprite, in addition to the bitmap image that contains the horizontal frame images:

  • Total number of frames

  • Current frame

  • Frame delay

  • Frame trigger

The next section puts code behind this sprite data as you add animated sprite support to the game engine.



Sams Teach Yourself Game Programming in 24 Hours
Sams Teach Yourself Game Programming in 24 Hours
ISBN: 067232461X
EAN: 2147483647
Year: 2002
Pages: 271

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net