Background
In past projects, I controlled music using the room creation code, e.g.:
audio_stop_all(); audio_play_sound(snd_music1,100,true);
This would stop all previously playing audio and start the new music track, looping it until the next room was created.
Functionally, this works fine, but I wanted a more centralized way of handling this so that if I wanted to make a change, I didn’t have to do it N number of places, equal to the number of rooms I created.
How It Works
I created an object called “obj_sound” and placed it in the first room, then I flagged it as ‘Persistent’ so that it would carry forward from room to room.
I added a Room Start event with the following code:
if room==rm_level1 { audio_play_sound(snd_lvl1,100,true); } if room==rm_level2 { audio_play_sound(snd_lvl2,100,true); }
At the start of the room, it checks to see if the name of the room matches “rm_level1” (the name of the first room/level), and if it does, it plays the sound “snd_lvl1” at a priority of “100”, and sets looping to “true.”
Once that was working, we needed to be able to stop the previous music track before loading the next one. This can be done using a Room End event:
audio_stop_all();
This stops all audio before the next room is loaded, preventing tracks from playing over each other.
Conclusions
There are other ways this could be handled, however…
- It works for what I need it to do (starts a looping music track that’s definable by room name).
- It’s centrally controlled.
- It does this in the simplest way I know of, and more importantly, I understand it!