1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Could you summarise what your game code looks like right now? In what event are you playing the sound effect? \$\endgroup\$ Commented Oct 7, 2015 at 18:08

4 Answers 4

1
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ I obviously know that, what is happening is it keeps playing again and again because it is executing it in the step event, every step it keeps playing. \$\endgroup\$ Commented Sep 24, 2015 at 17:06
  • \$\begingroup\$ I don't know Gamemaker, but can't you bind the sound to the jump end collision code? Or store the block that played a sound last and only play the sound if that block is no longer the stored sound block? Handling it in the collidion would be best I guess as it decouples it from your main game logic. \$\endgroup\$ Commented Sep 24, 2015 at 18:55
0
\$\begingroup\$

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; } 
\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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 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.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.