Creating an Automated Sprite

Now that you have a class that allows the user to control a sprite, it's time to add a class that will generate an animated sprite that moves on its own. Add a new class to your project by right-clicking on the project in Solution Explorer and selecting Add → Class. Name the class file AutomatedSprite.cs. Once the file is ready, mark the new class as a subclass of your Sprite class:

class AutomatedSprite: Sprite

Add the same XNA namespaces as before, but without the input namespace because you won't be gathering input from any devices in this class:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

Next, add two constructors for the AutomatedSprite class. These constructors will be identical to the ones used for the UserControlledSprite class:

public AutomatedSprite(Texture2D textureImage, Vector2 position, Point frameSize,
    int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed)
    : base(textureImage, position, frameSize, collisionOffset, currentFrame,
    sheetSize, speed)
{
}

public AutomatedSprite(Texture2D textureImage, Vector2 position, Point frameSize,
    int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed,
    int millisecondsPerFrame)
    : base(textureImage, position, frameSize, collisionOffset, currentFrame,
    sheetSize, speed, millisecondsPerFrame)
{
}

Your automated sprite will use the speed member of the base class to move around the screen. This will be done through an overridden direction property, because that property is abstract in the base class and therefore must be defined in this class. Create the override for the direction property as follows:

public override Vector2 direction
{
    get { return speed; }
}

Now you need to add the code that will make your automated sprite move. Because the direction property is represented by a Vector2 value, this property represents direction and speed for your automated sprite. Any direction in 2D space can be represented by a Vector2 value, and the magnitude (or length) of that vector indicates the speed of the object: the longer the vector is, the faster the automated sprite will move.

All you have to do is add the vector represented by the direction property to the position of your sprite, and the sprite will move in the direction of that vector at the speed indicated by the length of that vector.

Add to your AutomatedSprite class an overridden Update method that will move the sprite based on the direction property:

public override void Update(GameTime gameTime, Rectangle clientBounds)
{
    position += direction;

    base.Update(gameTime, clientBounds);
}

That's all there is to it! You now have an automated sprite class that will draw itself and update its position based on a 2D vector.

So far you have two different types of sprites, both deriving from a base class representing a generic animated sprite. In previous chapters, when you wanted to add a new sprite you had to add a number of variables and different settings to implement that new sprite. With this model, you can add a new sprite to your application with the addition of one new variable (either an AutomatedSprite or a UserControlledSprite).

However, thinking beyond just adding a few variables here and there for different sprites, let's look at a more modular approach. XNA provides us with a great tool for separating logical portions of code into different modules and allowing them to be plugged easily into a game and coexist. That tool exists in the form of a GameComponent.

In this next section, you'll learn about game components, and you'll build one that will be used to manage all of the sprites in your game.

Get Learning XNA 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.