Animated Sprites

From SDL.NET

Animated sprites are a powerful aspect of sprite programming in SDL.NET as they provide an intuitive method of animation in an easy way. SDL.NET's animated sprite system introduces three new classes: AnimationCollection, AnimationDictionary and AnimatedSprite. Please refer to the HeroExample for a demonstration.

Table of contents

AnimationCollection

The AnimationCollection class is a set of surfaces that act as frames of a single animation. They also hold how long the animation should take to animate as well as which direction to animate. To create an animation object, you can use one of its various constructors:

// Load the frames into the animation
AnimationCollection walk = new AnimationCollection();
walk.Add(new SurfaceCollection("walk.bmp", new Size(32,32)));

// Change the delay between frames
walk.Delay = 1000;  // wait 1 second each frame.

// Make a new animations
AnimationCollection run = new AnimationCollection();
run.Add(new SurfaceCollection("run.bmp", new Size(32,32)));
run.Delay = 250;

AnimationCollection stop = new AnimationCollection();
stop.Add(new Surface("stop.bmp"));

Please refer to the documentation on the AnimationCollection class for more information about functionality. We'll get back to how to use it later on.

AnimationDictionary

The animation dictionary is a way to hold a large collection of animations. This is held in a key-animation pair so it's quite similar to .NET's Hashtable class.

// Create the animation dictionary
AnimationDictionary animations = new AnimationDictionary();

// Add animations to the dictionary
animations.Add("Walk", walk);
animations.Add("Stop", stop);

// Change the delay of the walk animation in the dictionary to 2 seconds
animations["Walk"].Delay = 2000;

If you look at the documentation on the AnimationDictionary, you'll find many more features and functions to make life easier. Now that we have a way to store a collection of animations, we can then turn to the AnimatedSprite class and start animating.

AnimatedSprite

Once we know how to create an animation and have an AnimatedSprite object, we can animate it on screen. So lets first make our animation:

AnimationCollection walk = new AnimationCollection();
walk.Add(new SurfaceCollection("walk.bmp", new Size(32,32)));

Yes, we've seen this before, lets move onto something new:

AnimatedSprite hero = new AnimatedSprite(walk);

As you can see, we create a new animated sprite object named hero that has just one animation in it: walk. So lets animate it and paint it on the screen:

// After creation of hero
hero.Animate = true;

// In Events_Paint event:
Video.Screen.Blit(hero);

Now if you successfully created the application correctly, you should see an walking animation held in a Sprite interface. What if we wanted to have multiple animations with this sprite?

// Create the run animation
AnimationCollection run = new AnimationCollection();
run.Add(new SurfaceCollection("run.bmp", new Size(32,32)));
run.Delay = 250;

// Add it to the hero's animation dictionary
hero.Animations.Add("Run", run);

// Set the hero's current animation to run.
hero.CurrentAnimation = "Run";

We first created the run animation and added it to the sprite's animation dictionary and then set the hero's animation to Run. Since we didn't specify a name for the walk animation, it was set to "Default", being the default animation. To change back to the walk animation then, we'd just have to call:

hero.CurrentAnimation = "Default";

Conclusion

SDL.NET's animation system is very powerful as it provides a methodology of having multiple animations used by a single sprite. The Bounce Sprites example uses animated sprites to animate the sprites with a single animation, "Default". For more information, please see the documentation on the animated sprites.

Example Code

The HeroExample is an excellent demonstration of the Animated Sprite in which an animated character walks around the screen.

See Also