Game Development Reference
In-Depth Information
You'll notice that unlike
music_toggle
, we're not trying to stop the sound. This is
because we don't actually know which sounds are currently playing, and we can't stop
them all because it will stop any music that was playing as well. Most sound effects,
fortunately, will end by themselves at some point.
12.
We'll now need replacements for Game Maker's
sound_play
and
sound_loop
commands, just like we needed them for the
music_play
command. Add a new script
resource and call it
sfx_play
. Insert the following lines:
1:
{
2:
if ( global.playsfx ) sound_play(argument0);
3:
}
13.
Add another script resource and call it
sfx_loop
. Insert the following lines:
1:
{
2:
if ( global.playsfx ) sound_loop(argument0);
3:
}
14.
Open
obj_sfx_toggle
again and add a
Mouse
,
Left pressed
event. Insert an
Execute
Script
action and set
Script
to
sfx_toggle
.
15.
Now add a
Key Press
event for the
S
key. Insert another
Execute Script
action and set
the
Script
to
sfx_toggle
again. The S key now toggles sound effects.
16.
Our example file already had a few sound actions in it, but they're still being played by
the ordinary
Play Sound
action. We need to change those to use the script. Open
obj_spaceship
and select the
Press <Space>
event. Remove the
Play Sound
event for
snd_laser
and include the
Execute Script
event. Select
sfx_play
as the
Script
and enter
snd_laser
for
Argument0
.
17.
Now, select the
Press <Up>
event and again replace the
Play Sound
action with an
Execute Script
event, this time with
Script
sfx_loop
and
Argument0
snd_engine
. As
you may have guessed, if you're going to include this feature in the game, it helps to do
this early on to prevent you from having to replace countless sound actions!
18.
Finally, put an instance of
obj_sfx_toggle
into the room, next to
obj_music_toggle
.
Result:
Reference/Result/toggle_music_and_sfx.gmk
Search WWH ::
Custom Search