Shawn Hargreaves Blog
I wanted to add menu sounds to the Game State Management sample code I used in my AppWeek game, to make it play small clicks when changing the menu selection and bigger clicks when moving from one screen to another.
Menu selection sounds were easy: add SoundEffect.Play calls after the selectedEntry-- and selectedEntry++ lines in MenuScreen.HandleInput.
It wasn't so obvious how to add sounds for changing screen. I had maybe a dozen screens to deal with, but it felt like hundreds! If I tried to change each individual screen, I was worried I would miss some places and end up with bugs where one particular menu could be missing a sound if you cancelled it.
How about if I call SoundEffect.Play from the ScreenManager.AddScreen and GameScreen.ExitScreen methods? That catches everything with just two lines of code.
Trouble is, it caught a little too much:
What is the underlying pattern here?
I changed the InputState helper to remember whether MenuSelect, MenuCancel, or PauseGame had been pressed within the last frame, and made ScreenManager.AddScreen and GameScreen.ExitScreen only trigger sounds if one of these inputs was detected. I can't make up my mind whether this was a valid solution or an ugly hack (perhaps a little of both?), but it was certainly a tiny amount of code to get the result I wanted!
Would it make sense to add a couple bool properties to GameScreen called SoundOnExit and SoundOnCreation? This could default to true, and in the LoadingScreen screen (and the others you mentioned), just set these properties to false? Do you think that would work?
That would work for many screens, but not for things like MessageBoxScreen, which can often be brought up in response to a user button press (eg. the "are you sure you want to quit?" prompt), but also sometimes come up for other reasons (such as the "host ended the session" or "network error" messages).
I get an error when I try to open the Game State Management sample code. GS 3.1 tries to automatically convert it but fails because it says it cannot be converted. Something about permissions need to be changed on the solution file to allow modification....
Sounds like some of your files are set to read-only? You can change them to writable using Explorer.
Boy do I feel stupid! As it turns out, all of the files in the archive are indeed marked as read-only. I did not expect this, since the half dozen or so other samples I played with had worked just fine. I apologize for such a noob error on my part. Thanks for your time!