how do I make sounds on collisions play once?

I

itameio

Guest
I tried ALOT with different means but wasn't successful. . . I tried with variables and if-else statements, I tried based on speed, and some other stuff. but the sounds either keep playing or do not play at all.

what is the simplest way of doing this that actually works?.
 
M

Mordecai142

Guest
maybe have a variable which gets set to true each time you collide with whatever object (after the sound has played)
then have an alarm set it back to false maybe a few seconds later
play your sound only when the variable is false


just an idea
 
I

itameio

Guest
Can you show some code so we can see how you're trying to achieve this so we can help further?
I deleted the project file few days ago :(

But basically, I used a variable that would be set to true if collision happens, and false if it doesn't, and so when collision happens, the sound plays, and then the variable is set to false until it collides again.

but that gives the same result as not using the script at all, which is that the sound keeps playing as long as the two objects are collided.

I even used if speed>1 kinda expression to make the sound play when the two objects "crash" into each other, but the sound doesn't play at all.

So with the first one probably doesn't work because, I presume, the collision event is continuous, which means it keeps running again even after ending as long as the two objects are still collided, which makes it pointless to use variables.

maybe have a variable which gets set to true each time you collide with whatever object (after the sound has played)
then have an alarm set it back to false maybe a few seconds later
play your sound only when the variable is false


just an idea
That's why this won't work.

As for the second method, I guess it is because the objects stop when colliding with eachother, I presume the collision event only starts when their speed decreases to 0 and thus it is pointless as well.

aside from that, I have no Ideas.
 
M

mahmoud30pro

Guest
Create:
Code:
hit_play = true;
Step:
Code:
if (place_meeting(x, y, obj_ball)) {
   if (hit_play) {
      audio_play_sound(snd_ball_hit, 10, false);
      hit_play = false;
   }
}
else {
   hit_play = true;
}
I am in love with you bayb
 
K

KaelaOpalheart

Guest
I tried this, because I was having the same problems with some stationary spikes where the hit sound played once every frame the collision was still occurring. It did make the sound only play once, but.... ever. Any time I got hit again, even several seconds later, it never played again, all hits were silent. =/ Any suggestions?
 

TheouAegis

Member
if !place_meeting(x-hspeed,y-vspeed,other.id) play_sound()

So in the collision event, check if there wasn't a collision before and play the sound if not.
 
K

KaelaOpalheart

Guest
Thank you for the suggestion :D 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.
 
Top