Skip to Main Content
Learning XNA 3.0
book

Learning XNA 3.0

by Aaron Reed
November 2008
Beginner content levelBeginner
510 pages
16h 24m
English
O'Reilly Media, Inc.
Content preview from Learning XNA 3.0

Coding the SpriteManager

While your SpriteManager class is wired up and functional, it doesn't do anything yet. You can draw in your SpriteManager's Draw method, just as you can in your Game1 class. In fact, to clearly separate the sprite logic from the rest of your game, you'll want the SpriteManager to actually control all drawing of sprites. To accomplish that, you're going to have to add some code that will draw your sprites to that class.

The first thing you'll need is a SpriteBatch. While you already have a SpriteBatch object in your Game1 class, it makes more sense to create your own here for use within this class than to reuse that one. Only that way will you be able to truly isolate and modularize your game component code. Too much passing of data back and forth between the game and the game component will break that model.

In addition to adding a SpriteBatch variable, you need to add a few other variables: a list of Sprite objects that will hold all the automated sprites, and an object of type UserControlledSprite that will represent the player. Add each of these variables at the class level inside your SpriteManager class:

SpriteBatch spriteBatch;
UserControlledSprite player;
List<Sprite> spriteList = new List<Sprite>(  );

Just as the SpriteManager's Update and Draw methods are wired up to be called after your Game1 class's Update and Draw methods are called, the Initialize and LoadContent methods will also be called after the equivalent Game1 methods. You're going to need ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning XNA 4.0

Learning XNA 4.0

Aaron Reed
Beginning C# 7 Programming with Visual Studio 2017

Beginning C# 7 Programming with Visual Studio 2017

Benjamin Perkins, Jacob Vibe Hammer, Jon D. Reid

Publisher Resources

ISBN: 9780596154905Supplemental ContentErrata Page