5.1 Designing Your Classes 5.2 Creating a Sprite Class 5.3 Creating a User-Controlled Sprite Class 5.4 Creating an Automated Sprite Class 5.5 Game Components 5.6 Coding the SpriteManager 5.7 Cleaning Up 5.8 Making Them Move 5.9 What You Just Did 5.10 Summary 5.11 Test Your Knowledge: Quiz 5.12 Test Your Knowledge: Exercisezip Game1.cs
Define the base Sprite class by its functionality and then let specialized sprite inherit.
What activites must all sprites accomplish? These are the methods of the base sprite class.
This code will build on the code from Chapter 4. We add a new Class name Sprite.cs. We want this to be an abstract class to that when we instantiate obejcts we used a derived class.
Add two XNA namespaces to your new class, which youll need to enable the use of XNA objects:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
Based on Table 5-1, we have variables for each member of the class
Make sure you mark the protected variables appropriately, or the subclasses you build later will not work properly:
Texture2D textureImage; protected Point frameSize; Point currentFrame; Point sheetSize; int collisionOffset; int timeSinceLastFrame = 0; int millisecondsPerFrame; const int defaultMillisecondsPerFrame = 16; protected Vector2 speed; protected Vector2 position;
SpriteManager.cs
NOTE: difference between the two constructors - second one requires a millisecondsPerFrame variable, used to calculate the animation speed.
the first constructor calls the second constructor (using the this keyword) and pass to that constructor all of its parameters
In addition to the Update and Draw methods, youre going to want to add a property to this class that represents the direction in which this sprite is moving. The direction will always be a Vector2, indicating movement in the X and Y directions, but it will also always be defined by the subclasses (i.e., automated sprites will move differently than user-controlled sprites). So, this property needs to exist in the base class, but it should be abstract, meaning that in the base class it has no body and therefore must be defined in all subclasses.
Add the abstract direction property to the base class as follows:
public abstract Vector2 direction
{ get; }Theres one more thing to add to your Sprite base class: a property that returns a rectangle that can be used for collision detection. Add the following property to your Sprite class:
public Rectangle collisionRect { get { return new Rectangle( (int)position.X + collisionOffset, (int)position.Y + collisionOffset, frameSize.X - (collisionOffset * 2), frameSize.Y - (collisionOffset * 2)); } }
class UserControlledSprite: SpriteNext, youll need to add some XNA using statements. Use the same ones as you did in the Sprite base class, plus an additional one (Microsoft.Xna.Framework.Input) that will allow you to read data from input devices:
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input;Next, add the constructors for the UserControlledSprite class. These will be basically the same as the constructors for the Sprite class and will just pass the parameters on to the base class:
public UserControlledSprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed) : base(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed) { } public UserControlledSprite(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) { }UserControlledSprite.cs
class AutomatedSprite: SpriteAdd the same XNA namespaces as before, but without the input namespace because you wont be gathering input from any devices in this class:
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics;
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; } }
How do share your XNA code? When you turn your project in, you will be required to create a zip-file of the directory you developed in. I have created such a structure for the codes that we have covered in lecture, below:
Ch5AnimatedSprites.zip
Ch4AnimatedSprites.zip;
Ch3BasicSprite.zip,
Ch3MovingSprites.zip, and
Ch3AnimatedSprites.zip