Wow. That may have seemed like a lot of work, but I guarantee you'll be pleased
with what this has done for your code. Your sprite manager is complete and is
already wired up to your Game1
class. However,
you still have all the code that you added to Game1
in the previous chapters. You can now go into that class and
delete everything but the SpriteManager
code and
the generated code that was originally there. Your Game1
class should look something like this:
using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; using Microsoft.Xna.Framework.Net; using Microsoft.Xna.Framework.Storage; namespace AnimatedSprites { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SpriteManager spriteManager; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { spriteManager = new SpriteManager(this); Components.Add(spriteManager); base.Initialize(); } protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); } protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.White); base.Draw(gameTime); } } }
Compile and run your application, and you'll see the spinning rings object and four skull balls. You have control of the rings object via the keyboard, mouse, and gamepad, and if it collides with any of the skull balls, the game will end (see Figure 4-2). One thing to watch out for in this example is that when the game starts, the rings will be placed at the position of the mouse cursor. If that position happens to be on top of one of the automated sprites, the collision check will be true right from the beginning and the game will end. If you have that problem, move the mouse to the corner of the screen when you start the game. This is a small problem that won't cause any issues down the road because, as was mentioned before, the sprites that are being drawn currently are only there to test the sprite manager functionality.
Sweet! If you were wondering what the point of this chapter was, take a look at
the code in your Game1
class. Your application
now features a very solid object-oriented design, compared to what you were dealing
with previously. Look at the Draw
method and the
rest of the Game1
class. There's hardly anything
there! Even better, look at what it took to add an entire new animated sprite: one
line of code! Awesome! Remember how painful it was in the previous chapter to add a
new sprite? You had to add numerous variables and code and do lots of copying,
pasting, changing of variable names, etc. With that in mind, you can start to see
the benefit of a modularized approach using powerful tools such as XNA's GameComponent
s and a well-designed class
hierarchy.
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.