Tutorial 6: In this tutorial we will explore different ways to add sounds to your game.

Adding sounds to the lazer / bullet

In the Asset Browser resources menu:

  1. Create a sound Asset
  2. Name it sndPlayerLazer
  3. Click on the 3 dots in the window that appears
  4. From your spaceshooter-assets, browse to sounds folder and choose the sndLaser wave file
    adding sounds to gamemaker studio*Note: I have listed some websites where you can get sounds from at the end of this tutorial
  5. Inside the sounds settings windows you can change the volume of the sound, to lower it sound in the game if you want.
  6. In Target Options, change the Output to Stereo, if not already chosen.
  7. Open the Player Object from the Resources menu. Double click into the Key Press Z event you already created.
  8. Add the following code to the Key Press Z event, under the instance_create_layer() function, but inside the if statement:
    //audio function takes 3 arguments //reference to the soundfile, priority, loop(true / false) audio_play_sound(sndPlayerLazer,1,false); 

Play Music in the Game

To play music in your game, you must have a long enough music sound track that can loop on itself.

You can find copyright free music on lots of websites – one site I recommend is https://www.looperman.com/

  1. Go to the Resource menu and create a new sound called sndGameMusic
  2. Click the 3 dots and browse to the Asset Resources / Sounds folder
  3. Select the sndMusic file
  4. Name the sound to sndGameMusic
  5. Change the Volume to your liking and change the output to Stereo
  6. Next, double click into the Player object, objPlayer
  7. Add the following code to the Create event of the layer object:
     audio_play_sound(sndGameMusic,1,true);
  8. Run the game and see if the music plays

Music for each level / room of your game

  1. Create an objects that will represent the music you want for each level of your game.
  2. Add a Create event to the music object and use code similar to the below
    if(room!=EndRoom1){ //we want to pause music in the end room
    		audio_pause_all();
    	}else {
    		audio_play_sound(sndGameMusic,1,true);
    		}
    
  3. Add the music object into your game room

You can change the above code with an else if( ) statement to play different music based on each room

Website where you can acquire free sounds

https://www.sounds-resource.com/

https://freesound.org/

Similar Posts

3 Comments

  1. You can also have different music for each room by adding code like this to the Room Start event on your player object if you run into a problem with objects that represent your music:

    if room = Room1 {
    audio_play_sound(YourSoundFileName,1,true);
    }

    if room = Room2 {
    audio_stop_sound(YourSoundFileName);
    audio_play_sound(YourSoundFileName2,1,true)
    }

  2. You can randomize what audio files plays if you have different sounds for the lazer with this code:

    audio_play_sound(choose(sndPlayerLazer1, sndPlayerLazer2, sndPlayerLazer3), 1, false);

Leave a Reply

Your email address will not be published. Required fields are marked *