In my Game Maker Studio game, jumping on a musical block should play a note. Currently, if I step on it, the note keeps repeating.
How can I avoid that? I only want the note to play once.
In my Game Maker Studio game, jumping on a musical block should play a note. Currently, if I step on it, the note keeps repeating.
How can I avoid that? I only want the note to play once.
The function audio_play_sound as a third parameter, 'loop'. If this is set to true, then your sound will loop. Try setting this to false.
audio_play_sound(index, priority, loop);
Reference: http://docs.yoyogames.com/source/dadiospice/002_reference/game%20assets/sounds/audio_play_sound.html
Try putting a boolean condition before playing the sound then making it false when a collision happens.
Something like this:
if (place_meeting(x+1,y,objPlayer) || place_meeting(x,y+1,objPlayer) || place_meeting(x-1,y,objPlayer) || place_meeting(x,y-1,objPlayer) && noLooping == true) { audio_play_sound(index, priority, false); noLooping = false; } else { noLooping = true; } try this--
if !audio_is_playing(snd_xyz) { audio_play_sound(snd_xyz,..,..) }
I also had this problem once, though i could not solve it completely. The above code did some good though
I solved this problem without using any additional variables, just collision checking function. Assuming that in the real world when someone jumps you hear a sound only in the very moment contact with the ground occurs, I managed to get the following code which checks for collision in the current position and in the previous one. Objects name are only indicative:
var curr_coll = place_meeting(x,y+1,oBlock); var prev_coll = place_meeting(xprevious,yprevious+1,oBlock); Variables curr_coll is a boolean value that tells us if there's a collision with a block underneath the player at the current position. As well, prev_coll does the same, but assuming the player position in the previous game step.
So, we can have four different cases (CC: curr_coll, PC: prev_coll):
oBlock or whatever object, so the player is walking on top of it.That said, we can write our code to check for the needed values. You can use both a switch statement, or a chain of ifs, depending on your purpose. If we want to just play a sound only when landing on a musical key, we can write:
if ( (curr_coll==1) && (prev_coll==0) ) { audio_play_sound(sndNoteD,1,false); } So, our full code will be:
/// Step Event for object 'oPlayer' /// var curr_coll = place_meeting(x,y+1,oBlock); var prev_coll = place_meeting(xprevious,yprevious+1,oBlock); // Play sound if walk/jump on a musical block if ( (curr_coll==1) && (prev_coll==0) ) { audio_play_sound(sndNoteD,1,false); } As commented in code, this code may work both for jumping on top of or walking onto a musical block. If you want to distinguish between walking or jumping, you need to perform a couple of more checks, but nothing hard.