X hits on this document

297 views

0 shares

0 downloads

0 comments

75 / 102

}

We now have a class which represents an animation and encapsulates its functionality. We will now add an additional class to support sound playback.

162.

Add a new class under the “Utility” project folder and name it AudioManager.

163.

Add the following using statements to the beginning of the new class file:

C#

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Audio;

164.

We would like this class to be a singleton game component. First, alter the class to inherit from the GameComponent class:

C#

class AudioManager : GameComponent

{

}

165.

Add the following field to contain the AudioManager’s singleton instance:

C#

private static AudioManager audioManager = null;

166.

Add additional fields to the class:

C#

private SoundEffectInstance musicSound;

private Dictionary<string, SoundEffectInstance> soundBank;

private string[,] soundNames;

The above fields will be used to maintain sound related data.

167.

Add the following constructor to the class:

C#

private AudioManager(Game game)

   : base(game) { }

168.

Add a method and name it Initialize. This method will be used to initialize the singleton instance and register it with the game:

C#

public static void Initialize(Game game)

{

   audioManager = new AudioManager(game);

   if (game != null)

   {

       game.Components.Add(audioManager);

   }

}

Page | 75

Document info
Document views297
Page views297
Page last viewedSun Dec 11 02:46:45 UTC 2016
Pages102
Paragraphs3115
Words17333

Comments