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

Finishing Details

The first thing you’ll need to do in your Game1 class is add the following class-level variables:

ModelManager modelManager;
public Camera camera { get; protected set; }
public Random random { get; protected set; }
Texture2D attackTexture;
SoundEffect shotSound;

The first two variables are your model manager and camera game components. The random variable will be used for all the random calculations in your game. The attackTexture variable will be used to draw a small texture on the screen that the user will press to fire shots. The final variable added is the sound effect for firing shots.

Next, you’ll want to initialize your random variable in the constructor of your Game1 class with the following code:

// Initialize random number generator
random = new Random();

Then add the following code to your Initialize method to initialize and add both of your game components:

// Initialize Camera
camera = new Camera(this);
Components.Add(camera);

// Initialize model manager
modelManager = new ModelManager(this);
Components.Add(modelManager);

Then you’ll need to add the following code to your LoadContent method to load your texture and audio resources:

// Load textures
attackTexture = Content.Load<Texture2D>(@"textures\attack");

// Load audio
shotSound = Content.Load<SoundEffect>(@"audio\shot");

Now, in your Update method, add some code that will fire shots when the user presses the texture on the screen. Add the following code just before the call to base.Update:

// Did the user ...

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