Time for action – playing sound effects
To add a sound effects manager to Mars Runner, perform the following steps:
1. Add a new
using
direction at the beginning of theMarsRunnerPlayScreen
class to allow us to access the XNA Audio classes as follows:using Microsoft.Xna.Framework.Audio;
2. In the
LoadContent()
method of theMarsRunnerPlayScreen
class, load the sound effects and add them to the sound effect manager's dictionary as follows:SFXManager.AddEffect( "Explosion", content.Load<SoundEffect>(@"Sounds\Explosion1")); SFXManager.AddEffect( "Jump", content.Load<SoundEffect>(@"Sounds\Jump")); SFXManager.AddEffect( "PlayerShot", content.Load<SoundEffect>(@"Sounds\Shot1")); SFXManager.AddEffect( "EnemyShot", content.Load<SoundEffect>(@"Sounds\Shot2"));
3. Inside the
FirePlayerShot()
method in theMarsRunnerPlayScreen
class, call theSFXManager.Play()
method just after resetting theplayerShotTimer
to0.0f
as follows:SFXManager.Play("PlayerShot");
4...