Game Development Reference
In-Depth Information
4.
Add another Script resource called
music_toggle
and insert the following lines:
1:
{
2:
global.playmusic = !global.playmusic;
3:
if ( global.playmusic ) sound_play(global.music)
4:
else sound_stop(global.music);
5:
image_index = global.playmusic;
6:
}
The value of
global.playmusic
should either be
1
(true) or
0
(false). Rather than using
an
if
statement to check and change, we're using the Boolean operator
!
(
NOT
),
because it flips the value around for us. Line 3 checks if we should either play or stop
playing the music (line 4) and line 5 changes the subimage.
5.
Open
obj_music_toggle
again and add a
Mouse
,
Left pressed
event. Insert an
Execute
Script
action and set
Script
to
music_toggle
.
6.
Add a
Key Press
event for the letter
M
and include another
Execute Script
action.
Again, set
Script
to
music_toggle
. We can now either click the object or press the M
key to toggle the music.
7.
Place an instance of
obj_music_toggle
in the top-right of the test room.
8.
It can't work without any music, so we need to make sure we play some music in the
same room. Open
obj_spaceship
and add an
Other
,
Room Start
event. We use this
event because it is executed after all
Create
events have been handled, so the
global.playmusic
variable will already have a value. Here comes the most important
part—we're no longer going to use the
Play Sound
action to play sounds or music.
Include an
Execute Script
event and set
Script
to
music_play
. Set
Argument0
to
snd_music
. Notice there is no longer a drop-down menu, so we'll have to enter the
correct values ourselves.
9.
Now, add an object called
obj_sfx_toggle
, set the
Sprite
to
spr_sfx_toggle
, and the
Depth
to
-1000
. The toggle for sound effects works in much the same way as
obj_music_toggle
, as you'll see shortly.
10.
Add a
Create
event, include an
Execute Code
action, and insert the following lines:
1:
{
2:
if ( !variable_global_exists('playsfx') ) global.playsfx = true;
3:
image_index = global.playsfx;
4:
image_speed = 0;
5:
}
11.
Add another Script resource and call it
sfx_toggle
. Insert the following lines:
1:
{
2:
global.playsfx = !global.playsfx;
3:
image_index = global.playsfx;
4:
}
Search WWH ::
Custom Search