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

Adding Your Particle Engine to Your Game

Now that you have an effect and a particle engine, you need to modify your code to create explosions when ships are hit.

Open the ModelManager class, and let’s get to work on adding some cool explosions to your game. Add the following class-level variables:

List<ParticleExplosion> explosions = new List<ParticleExplosion>();
ParticleExplosionSettings particleExplosionSettings = new ParticleExplosionSettings();
ParticleSettings particleSettings = new ParticleSettings();
Texture2D explosionTexture;
Texture2D explosionColorsTexture;
Effect explosionEffect;

These variables are all pretty self-explanatory: you have a list of ParticleExplosions so your class can update and draw them; an instance of both settings classes for your particles; and Effect and Texture2D objects for drawing your particles, an explosion texture, an explosion colors texture, and an effect.

Next, you’ll need to load the resources for your particles, set the current technique of your particle effect, and set the texture for that effect as well. Modify the LoadContent method in your ModelManager class as follows:

protected override void LoadContent() { // Load explosion textures and effect explosionTexture = Game.Content.Load<Texture2D>(@"Textures\Particle"); explosionColorsTexture = Game.Content.Load<Texture2D>(@"Textures\ParticleColors"); explosionEffect = Game.Content.Load<Effect>(@"effects\particle"); // Set effect parameters that don't change per particle explosionEffect.CurrentTechnique ...

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