I thought I would implement something relatively simple tonight, so I decided to work on audio.  I already have two songs that play for the title screen and level screen, but the logic is built into my main game class which isn’t ideal.

For this game, I decided to take a new approach with the music.  Instead of manually starting and stopping songs when needed, I decided to assign a Song to each Screen class.  When the current Screen is changed, then the music is changed to match that screen.  I may have to make some exceptions, for instance in the case of a boss battle.  The player would still be on the GameScreen instance, but the music would need to change.

To implement this, I added a new Jukebox class to my ResistorKit library.  This will hold the references to all songs and sound effects.  Songs and sound effects are still loaded in the LoadContent method of the main game class, but after each has loaded it is added to the Jukebox object using the addSong and AddSoundEffect methods that I created.  The one drawback is that each Screen needs a reference to the Jukebox.  I can set the Jukebox in the current screen on each call to Update, but I will need to double check in the Screen to ensure that the Jukebox reference is not null before calling any methods on it.  I also created an instance variable in the Jukebox class to keep track of the currently playing Song.  The Song is only changed if the current song instance variable is changed, since there may be multiple screens with the same Song.  Overall, I like this method much better because it keeps the references to all audio objects in one class.  The beauty of this is that a Song identifier only has to be set for a Screen once, and then the music for that screen is automatically played whenever that screen becomes active.

In my main game class, I created an enum called GameSongs which holds identifiers for all of the Songs.  This is much cleaner than all of the int constants that I have been using.  I will want to go back and convert my Texture2D constant int identifiers to enums as well.  The only problem is that my Jukebox class in ResistorKit is not aware of the enum in my game.  I’d rather not put the enum or a reference to it in my library, because that would prevent other projects with different song identifiers from using the library.  I was able to solve this problem by casting the enum identifier to an int when calling the addSong method on the Jukebox object.

After adding the Jukebox to the game and making the necessary updates, I was able to use it to play the sound files.  One issue that I noticed was that since the Attract screen is displayed first, the setCurrentScreen method never gets called since it does not transition from another screen state, so the music wasn’t playing.  As a quick fix, I set the next screen state to the Attract screen in the constructor of the Attract screen, just so that the setCurrentScreen method gets called when it is first displayed.  This change made the music play correctly.  Since the Title Screen has the same Song, the music continued to play without breaking during the transition from the Attract screen to the Title screen which is what I wanted.


Finally, I pulled one of the sound effects from Resistor to use for the gun.  I just needed a temporary sound effect to use to see if the play method for sound effects in Jukebox is working.  I have to say, the Windows Media Player for Windows 8 is really horrible, because it is full screen with no way to change the size.  The Windows 8 music interface forces you to look at a bunch of advertisements from musicians I’ve never heard of and musicians that I really don’t like.  Fortunately, installing Audacity is a really fast and simple process.


One thing that is easy to forget is to set the Jukebox object in the Screen class to protected, so that the objects that subclass the Screen class can use the Jukebox object.  Fortunately, this only has to be set in the library, so the Jukebox will automatically be accessible to any implemented subclass.  Also, it is necessary to set the enum in the main game class to public so that any implemented Screen subclass can reference the sound effect identifiers.  However, the enum does not need to be set to static or constant.

After making these changes, the gun sound effect plays correctly when pressing the fire button.  However, the sound effect would play even if the player does not have a gun equipped and a projectile is not shot.  I would rather not put the sound effect playing code into the model.  I noticed that I already had a LifeTime variable defined in the projectile class, which keeps track of how many frames the projectile has lived.  Therefore, I just added a getter method for that variable, and I play the sound effect if the LifeTime variable is one (first frame).  I moved the code to play the sound from the button press method to the loop that draws the projectiles, and now it only plays the sound if a projectile is shot.  Having the sound play on the first projectile frame will give me the ability to play different sounds based on the projectile type (fire, ice, et cetera), since the projectile is already instantiated.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: