Game in a Day stumbling blocks: Songs in XNA?
Well, I decided its as good a time as ever to begin proper feedback on what occured during the Game in a Day challenge I set, and for starters I decided to find out about the one thing I actually asked about on Twitter during the challenge.
If you have been following it, you will remember I posted about preparation for rapid development for the day, and as part of it wrote a simple sound handling class. The class is unfortunately still untested, but in theory it should work as is, also allowing for playing of actual background music. During the challenge though, I kind of realised that I didnt feel that SoundEffect instances in XNA should be used for playing songs in XNA. After resorting to tweeting about it, I left it on the day, but now that Ive been less restricted time wise Ive looked around and found myself some example, and a better understanding of using Song data types.
What I found was from the XNA creators club forums (which I could hve used on the day had I had better internet then my phone) and Mark Baker has to take the kudos for the code I looked at.
Essentially the sound handling class would need to be extended to have a collection of songs and play them whenever they are needed. This in itself is simple to do, and shouldnt set you back more thn a few minutes of implementing to have up and running.
If you want to extend the sound handling class to include very basic song playing, you would need to do the following:
- Add a list to hold all your Song objects
- Add a method that can be used to load a song
- Add a method to make the media player play a song
Now the above is fine for the very very basic playing of songs, but personally I would also do the following:
- Add a state to the sound manager (FadeIn, FadeOut)
- Add a method that changes the volume according to the state of the sound manager (FadeIn, FadeOut)
- Add a method to set a bool to true and to keep track of the Song you want to play next
- Add to the method for playing songs to set the state to FadeOut and that songs are changing (as in the above point)
- Add in to the update method a check for if we are changing a song, and if we are and the volume gets to zero to set the state to FadeIn and the bool to false
- Add in a check to see if the MediaPlayer.State == MediaState.Stopped, and if it has stopped to start the next song and fade it in
Now all of the above should help you end up with a fairly decent song playing structure for your game that operates on its own (to an extent) allowing you to add mood and ambience to your game. It also will help me for the next Game in a Day challenge, at the start of which I will update my sound handling class to include Song handling. So that is all that there is to the first part of looking back at the Game in a Day challenge.
- edg3's blog
- Login or register to post comments