© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023
B. TyersGameMaker Fundamentalshttps://doi.org/10.1007/978-1-4842-8713-2_14

14. Sounds

Ben Tyers1  
(1)
Worthing, West Sussex, UK
 

Sounds and music within a game are very important. Using the right style of music and sound effects can alter how the player perceives the game.

Sounds can be used for
  • Damage.

  • Clicking menu items.

  • Providing audio feedback to the play.

  • Setting the scene with appropriate background music.

  • Alongside graphical effects.

  • Telling the player something has been done correctly.

Sound Effects

Go ahead and load in the sound snd_bell, as shown in Figure 14-1.

A property dialog box of the sound s n d bell. It consists of name, volume adjustment, timer, conversion for O G G, attributes, target option of output, quality, sample and bit rate, and Audio group.

Figure 14-1

Showing snd_bell imported

Create an object, obj_test and put in the following code in a Step Event:
/// @description Play sound on S
if keyboard_check_pressed(ord(“S”))
{
      audio_play_sound(snd_bell,1,false);
}

This will play the sound snd_bell each time S is pressed. The false tells the game not to loop the sound and only play it once through. Setting to true would loop the sound.

Place an instance of this object into the room and test. You’ll notice that the bell will play and overlap each time you press S – which may be an undesired effect depending on how you want your game design. You have a couple of options to prevent this from happening.

Method one is to check if a sound is playing already, and if it is, don’t play the sound again.

Which would look like this:
/// @description Play sound on S
if keyboard_check_pressed(ord("S"))
{
      if !audio_is_playing(snd_bell) audio_play_sound(snd_bell,1,false);
}
A second option is to stop the playing sound and play again from the beginning, which would look like this:
/// @description Play sound on S
if keyboard_check_pressed(ord("S"))
{
      if audio_is_playing(snd_bell) audio_stop_sound(snd_bell);
      audio_play_sound(snd_bell,1,false);
}

Music

Add a new sound, snd_music_1 and load music_box from the resources.

Put the following code in to a Create Event of object obj_test:
/// @description Play music on loop
audio_play_sound(snd_music_1,1,true);

plays a music track on a continuous loop.

If you test now, you will hear the music playing on loop.

You can assign a sound to a variable, making it easier to refer to so you can perform actions on the sound, for example, by changing the Create Event code to:
/// @description Play music on loop
snd_bg_music=audio_play_sound(snd_music_1,1,true);

which would start the music and create a reference to it.

You can adjust the volume a sound plays at, for example, by adding the following in a Step Event:
if keyboard_check_pressed(ord("V"))
{
      audio_sound_gain(snd_bg_music,0.25,0);
}

which would change the volume of the playing to music to 25%.

Test your game and press V to change the volume.

You can check if a sound is playing, and stop it if it is. One method:
if keyboard_check_pressed(ord("D"))
{
      if (audio_is_playing(snd_bg_music))
      {
            audio_stop_sound(snd_bg_music);
      }
}

checks if key D is pressed and then stops the sound if it is playing.

Pausing and Resuming Audio

Additional commonly used functions are:
audio_pause_sound();
audio_resume_sound();
audio_stop_all();
You can add a pause and resume system with:
if keyboard_check_pressed(ord("P"))
{
      if audio_is_playing(snd_bg_music)
      {
            audio_pause_sound(snd_bg_music);
      }
}
if keyboard_check_pressed(ord("R"))
{
      if audio_is_paused(snd_bg_music)
      {
            audio_resume_sound(snd_bg_music);
      }
}
Note

If you stop a sound, it cannot be resumed. You can only resume paused audio.

Basic Projects

  1. A)

    Make a basic jukebox that can select a track, pause, and resume the current track.

     
  2. B)

    Set an object to play a random sound upon collision with a wall.

     

Advanced Projects

  1. C)

    Play music from an object that changes volume, depending on how close the player is.

     

Useful Functions

You get the current track position:
position=audio_sound_get_track_position(snd_bg_music);
Conversely you can set a position:
audio_sound_set_position(snd_bg_music,2);
You can get the length of a track in seconds with:
track_length=audio_sound_length(snd_bg_music);

Summary

You can now add in sounds and music and perform basic functions such as playing, pausing, resuming, and starting. You can check whether a sound is currently playing or not.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.46.18