O'Reilly logo

Learning XNA 4.0 by Aaron Reed

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Animation

As mesmerizing as it is to sit and watch moving, bouncing XNA logos, that’s not exactly the reason you’re reading this book. Let’s get to something a little more exciting by animating some sprites.

Tip

The code for this section of the chapter is available with the source code for the book under Chapter 3 and is titled AnimatedSprites.

As discussed earlier in this chapter, animation in 2D XNA games is done much like a cartoon flipbook. Animations are made up of a number of standalone images, and flipping through the images in a cycle causes them to appear animated.

Typically, sprite animations are laid out in a single sheet, and you pull out individual images from that sheet and draw them on the screen in a specific order. These sheets are referred to as sprite sheets. An example of a sprite sheet is included in the source for this chapter, in the AnimatedSprites\AnimatedSprites\AnimatedSpritesContent\Images folder. The sprite sheet is named threerings.png and is shown in Figure 3-8.

Sample sprite sheet (threerings.png)

Figure 3-8. Sample sprite sheet (threerings.png)

In each of the previous examples, you have drawn a sprite by loading the image into a Texture2D object and then drawing the entire image. With a sprite sheet, you need to be able to load the entire sheet into a Texture2D object and then pull out individual sprite frames to draw as you cycle through the animation. The overload for SpriteBatch.Draw that ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required