Im making a small game in Game Maker Studio. I have a piece of code that moves the player to the next room when he touches obj_star: this works. I have another line that tries to play sound_collect when the star is touched, but the game just moves to the next room without playing the sound. My code is as follows:
//Get the player's input
key_right = keyboard_check(vk_right);
key_left = -keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_space);
//React to inputs
move = key_left + key_right;
hsp = move * movespeed;
if (vsp < 10) vsp += grav;
if (place_meeting(x,y+1,obj_wall))
{
vsp = key_jump * -jumpspeed
}
//Horizontal Collision
if (place_meeting(x+hsp,y,obj_wall))
{
while(!place_meeting(x+sign(hsp),y,obj_wall))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;
//Vertical Collision
if (place_meeting(x,y+vsp,obj_wall))
{
while(!place_meeting(x,y+sign(vsp),obj_wall))
{
y += sign(vsp);
}
vsp = 0;
}
y += vsp;
//Sounds
if (place_meeting(x+hsp,y,obj_star))
{
sound_play(sound_collect)
};
At first, you need to use the new audio engine and functions
audio_...
. All functionssound_...
is legacy and don't work with the new audio engine.At second, I not sure that
is a right way. For example, if you destroy
obj_star
after touching then you can play the sound in theDestroy
event of theobj_star
.