Thank you for the suggestion
Unfortunately it didn't work, or at least I couldn't figure out the right way to implement it, probably. I did however end up finding an answer on another info source, and upon testing it does work! I will paste their post in its entirety, because I am definitely not going to try to pretend this is my own answer. For context, they were answering someone else asking about the issue of repeating sounds in relation to jumping on piano keys, hence the references to notes and such in his example. Everything after this is what they wrote.
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):
- CC = 0, PC = 0 : no collision both currently and previously, so the player has jumped and is in mid-air;
- CC = 0, PC = 1 : no collision now, but collision detected in the previous step. This means the player has just performed a jump;
- CC = 1, PC = 0 : there's a collision at the current step, and no collision a game step before. So, the player was in mid-air and is currently hitting the ground. Now you can play the proper musical sound;
- CC = 1, PC = 1 : there's always a collision with a given 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 switchstatement, 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.