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

Creating a Sprite Class

Now you can go ahead and get started on your Sprite base class. What might you want to include in that class? Table 5-1 lists the members, and Table 5-2 lists the methods.

Table 5-1. Members of your Sprite class

Member

Type

Description

textureImage

Texture2D

Sprite or sprite sheet of image being drawn

position

Vector2

Position at which to draw sprite

frameSize

Point

Size of each individual frame in sprite sheet

collisionOffset

int

Offset used to modify frame-size rectangle for collision checks against this sprite

currentFrame

Point

Index of current frame in sprite sheet

sheetSize

Point

Number of columns/rows in sprite sheet

timeSinceLastFrame

int

Number of milliseconds since last frame was drawn

millisecondsPerFrame

int

Number of milliseconds to wait between frame changes

speed

Vector2

Speed at which sprite will move in both X and Y directions

Table 5-2. Methods of your Sprite class

Method

Return type

Description

Sprite(...) (multiple constructors)

Constructor

Sprite constructor method

Update(GameTime, Rectangle)

void

Handles all collision checks, movement, user input, and so on

Draw(GameTime, SpriteBatch)

void

Draws the sprite

This chapter will build upon the code that you created in Chapter 4. Open the code from that chapter and add a new class to your project by right-clicking on the project in Solution Explorer and selecting AddClass. Name the new class file Sprite.cs.

Because you’ll probably never find a reason to create an instance of the Sprite class, ...

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