XNA 2D Game Tutorial (Part 7)

November 11, 2010

tags: , , , , ,
no comments

Previous posts in this series:

The game as it is now is not yet challenging – there are no enemies to threaten the player – she simply flies through space, shooting at nothing. We’ll deal with some enemies in the next part, but first let’s consider sound.

Sound is a critical part of any game. Just for kicks, play your favorite action game with the volume off – you’ll get bored or irritated pretty quickly. Sound is not just for fun, though; sometimes it’s a critical ingredient, indicating to the player that something important has happened without the player needing to focus in any particular point on the screen.

So how do we play sound effects or music in XNA? There are basically two ways: the first is to use XACT (Microsoft Cross Platform Audio Creation Tool), which allows great control over the sounds you want to play. The second route is to use a (pretty) simple API provided originally for the Zune (which does not support XACT), but can be used with any XNA supported device. We’ll take the second approach in this tutorial.

The first order of business is to add audio assets to the content project, much the same way we added images. XNA supports playing WAV files (and some other less frequently used formats) as a sound effect. It also supports playing MP3 files (more suitable for background music).

Add a folder named “Audio” to the content project (sibling of “Images”). Right click the new folder and select Add –> Existing Item and navigate to a WAV file. For our purposes, we’ll add a WAV file that would be the sound when the player fires one of its missiles/bullets.

Next, we need to load that sound into a SoundEffect object, very similar to other assets using the ContentManager.Load<> generic method (in the LoadContent method of the PlayerComponent class for our purposes):

_bulletSound = Game.Content.Load<SoundEffect>("Audio/bullet");


Where _bulletSound is a field of type SoundEffect.

The SoundEffect type has a Play method, and it’s tempting to simply use it to play the sound. However, that’s not recommended. The reason is that a call to Play is a kind of “singleton”; that is, a single sound instance can be played at one time. So, if the player fires two bullets in close succession (and the sound effect is not too short), a second Play would not create a separate, independent sound. The proper way to do this is use the SoundEffect.CreateInstance method that returns a SoundEffectInstance object, representing an independent sound instance.

Each SoundEffectInstance can be separately played and configured (e.g. different volume).

The easiest way to handle the sound effect for the player’s bullets (although a bit wasteful) is to create a same length array of SoundEffectInstance references, initialize them using SoundEffect.CreateInstance and playing when appropriate (e.g. in the CreateBullet helper method).

_bulletSoundInstance[i] = _bulletSound.CreateInstance();

_bulletSoundInstance[i].Volume = .7f;


This initializes the sound effect instances inside the loop that creates the bullet sprites. To play, we’ll call SoundEffectInstance.Play in CreateBullet after the new bullet has been setup properly:



That’s about it! We have sound!

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>