Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Building your First Mobile Game using XNA 4.0

You're reading from   Building your First Mobile Game using XNA 4.0 A fast-paced, hands-on guide to building a 3D game for the Windows Phone 7 platform using XNA 4.0

Arrow left icon
Product type Paperback
Published in Jan 2013
Publisher Packt
ISBN-13 9781849687744
Length 158 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Thomas Goussaert Thomas Goussaert
Author Profile Icon Thomas Goussaert
Thomas Goussaert
Brecht Kets Brecht Kets
Author Profile Icon Brecht Kets
Brecht Kets
Arrow right icon
View More author details
Toc

Playing sound


Playing non-positional sounds, being regular sounds, is fairly straight forward. XNA contains a class called SoundEffect. This class can load sounds and play them. Let's jump into a sample.

SoundEffect

The code files contain all the audio samples we need. Add them to your content project. The importer and processor will be automatically detected by XNA Game Studio, depending on the file type. WAV files will use the SoundEffect processor by default, while MP3 files will use the Song processor by default.

Then in the MainGame, we need to create a field that will hold the sound. This field will be of type SoundEffect. Also add a counter of type int. We will play the sound if the counter reaches 100.

SoundEffect jumpSound;
int counter = 0;

In the LoadContent method, we have to load the sound. This is done in the same fashion as loading other content.

jumpSound = Content.Load<SoundEffect>("Jump");

Finally, in the update, we'll increment the counter. If the counter reaches 100, we...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image