X hits on this document

285 views

0 shares

0 downloads

0 comments

76 / 102

169.

Add a new method and name it LoadSounds. This method will be responsible for loading a predefined set of sound assets:

C#

public static void LoadSounds()

{

   string soundLocation = "Sounds/";

   audioManager.soundNames = new string[,] {

                   {"CatapultExplosion", "catapultExplosion"},

                   {"Lose", "gameOver_Lose"},

                   {"Win", "gameOver_Win"},

                   {"BoulderHit", "boulderHit"},

                   {"CatapultFire", "catapultFire"},

                   {"RopeStretch", "ropeStretch"}};

audioManager.soundBank = new Dictionary<string, SoundEffectInstance>();

   for (int i = 0; i < audioManager.soundNames.GetLength(0); i++)

   {

SoundEffect se = audioManager.Game.Content.Load<SoundEffect>(

           soundLocation + audioManager.soundNames[i, 0]);

       audioManager.soundBank.Add(

           audioManager.soundNames[i, 1], se.CreateInstance());

   }

}

170.

Add the following methods to the AudioManager class:

C#

public static void PlaySound(string soundName)

{

   // if the music sound exists, start it

   if (audioManager.soundBank.ContainsKey(soundName))

       audioManager.soundBank[soundName].Play();

}

public static void StopSound(string soundName)

{

   // if the music sound exists, start it

   if (audioManager.soundBank.ContainsKey(soundName))

       audioManager.soundBank[soundName].Stop();

}

public static void PlayMusic(string musicSoundName)

{

   // stop the old music sound

Page | 76

Document info
Document views285
Page views285
Page last viewedFri Dec 09 22:19:14 UTC 2016
Pages102
Paragraphs3115
Words17333

Comments