Intro to XNA on Windows Phone Part 5

This is the fifth part of a series where I am taking a presentation I did on using XNA with Windows Phone and stepping through it, elaborating as necessary. Here are the original assets for the presentation, including source code and demo script.

This series is all about demystifying game development and breaking it down into simple steps:

- Draw Stuff
- Handle and act on input
- Play sounds
- Monetize
- Stick with it!

So far we’ve made it through the first two steps and we’re on to playing sounds. This post starts where we left off last time: http://www.billreiss.com/wp-content/uploads/2012/04/MyFirstGame_part4_completed.zip

Step 3: Play Sounds

There are two major types of sounds in XNA, one is for sound effects and the other is for longer sounds that is mainly used for background music and cut scenes. Both are easy to get the basics working in while providing advanced capabilities if needed. Let’s look at sound effects first.

First of all, add the sound effect file to your content project. You can use this file:

http://www.billreiss.com/wp-content/uploads/2012/04/PlayerJump.wma

Call it PlayerJump.wma. When you add it to your content project and then look at the properties window it’ll look something like this:

image

Since this is a WMA file it defaults to the Song content processor. Since we want to use it as a sound effect, you need to change the content processor to Sound Effect:

image

If you were to bring in a WAV file, it would default to Sound Effect since this is the native format for sound effects.

Add a field to the Game class for the sound effect:

SoundEffect playerJumpSound;

Then in the LoadContent method, we do pretty much the same thing as for images but specify the SoundEffect type instead:

playerJumpSound = Content.Load<SoundEffect>("playerJump");

And finally in the Update method, we can play the sound effect when we detect to start a jump:

if (isPressed && !jumping)
{
    jumping = true;
    currentPlayerAnimationDelay = 0;
    velocityY = startVelocityY;
    playerJumpSound.Play();
}

 

Run the app and when the player jumps you should now hear the sound play. Next let’s take a look at background music. For this we can use the Song and the MediaPlayer classes.

Here is our looping music:

http://www.billreiss.com/wp-content/uploads/2012/04/MusLoop.wma

Add it to the content project and this time we can keep the default content type of Song. Now declare a new field in the Game class to hold the Song:

Song musicLoop;

Then in the LoadContent method:

musicLoop = Content.Load<Song>("MusLoop");
MediaPlayer.IsRepeating = true;
MediaPlayer.Play(musicLoop);

Now if you run the game again you should hear looping background music.

One issue with sound is finding good sound effects and music that are licensed for use in mobile applications. One good source I’ve found for inexpensive and royalty free sounds and other assets is http://www.pond5.com

Here is the source code for our current progress:

http://www.billreiss.com/wp-content/uploads/2012/04/MyFirstGame_part5_completed.zip

In the next post we’ll look at adding the Microsoft Advertising control to our game.

  • Jeff Bane

    Where’s the next part, I’m at the point where I want to add ads to my game!!