• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

How to stop sound from overlapping and repeating in step event?

P

Pitu

Guest
Hey so I'm having some trouble with playing sounds, When the player walks i want the aud_walking sound to play, but what happens is that when it is in the step event (at least I THINK it's because it's in the step event) the sounds keeps overlapping on its self (tried playing with loop variable setting to true and false) but nothing helps? How would i implement this?

Code:
if(hspd > 0){
    audio_play_sound(aud_walking, 1, true);
} else if(hspd < 0){
    audio_play_sound(aud_walking, 1, true);
} else{
    audio_stop_sound(aud_walking);
}
"hspd" is the variables that determine if the player is gaining horizontal speed in each direction so like if he is walking to the left the hspd will be in negative and if right it will be positive
this code is placed in the step event (NOTE: I also tried to give it conditionals but that didnt help either like sound_is_playing = false etc..), I really dont want to use the keyboard events so please help me implement it somehow, the sound doesn't even play in the create event btw
 

TheBroman90

Member
Code:
if(hspd > 0){
if !audio_is_playing(aud_walking)
audio_play_sound(aud_walking, 1, true);
} else if(hspd < 0){
if !audio_is_playing(aud_walking)   
audio_play_sound(aud_walking, 1, true);
} else{
audio_stop_sound(aud_walking);
}
This should work.
 
P

Pitu

Guest
Thanks for your reply, but it gives me the same result the audio is all overlapping and ughh it almost sounds like a machine gun xD
 

NightFrost

Member
Are you possibly issuing play commands for the sound elsewhere in your code? It would explain why the audio_is_playing() check is having no effect. Also, does it stop when you stop moving (hspd is zero)?
 
P

Pitu

Guest
Are you possibly issuing play commands for the sound elsewhere in your code? It would explain why the audio_is_playing() check is having no effect. Also, does it stop when you stop moving (hspd is zero)?
Hey it the audio stops all togheter when i stop moving (so hspd does equal to zero when im not touching the controls) however as soon as i move its a jumble of overlapping aud_walking sound, and no im not using any other sounds at the moment and it's no where else in the code, just in the step event
 

TheBroman90

Member
I tested it myself, with separate codes for keyboard checks and speed like you and for me the sound works fine.

Code:
if keyboard_check(vk_right)
{
    hspeed = 1;
}
else if keyboard_check(vk_left)
{
    hspeed = -1;
}
else
hspeed = 0;


if hspeed > 0
{
    if !audio_is_playing(aud_walking)
    audio_play_sound(aud_walking, 1, true);
}
else if hspeed < 0
{
    if !audio_is_playing(aud_walking)
    audio_play_sound(aud_walking, 1, true);
}
else
audio_stop_sound(aud_walking);
What does your movement code look like?
 

NightFrost

Member
I tested the code too, and it appears to work as it should. I can only imagine the sound you are using is so short it sounds like a machinegun when looped.
 
P

Pitu

Guest
Alright ill post my entire code
both are under obj_player (all the code in my game for now)
PS. the audio is like a 6 second long mp3 of footsteps sound

CREATE EVENT:
Code:
///Initialize Variables
grav = 1;
spd = 4;
jspd = 20;
hspd = 0;
vspd = 0;

//Stop animation
image_speed = 0;
STEP EVENT:
Code:
//Get the player's input
key_right = keyboard_check(ord('D'));
key_left = keyboard_check(ord('A'));
key_jump = keyboard_check_pressed(ord('W'));


//Check for collisios
if(place_meeting(x, y+2, obj_wall))
{
    vspd = 0;
   
    //Jumping
    if(key_jump){
       
        vspd = -jspd;
    }
} else{
    //Move down with gravity
    if(vspd < 10){
        vspd += grav;
    }
}
//Moving to the right
if(key_right){
 
    hspd = spd;
   
}
//Moving to the left
if(key_left){

    hspd = -spd;

}


//Check for not moving
if((!key_right && !key_left) || (key_right && key_left)){

    hspd = 0;
   
}
//Horizontal collisions ADD IN SCRIPT
if(place_meeting(x+hspd, y, obj_wall)){
    while(!place_meeting(x+sign(hspd), y, obj_wall)){
        x+= sign(hspd);
    }
    hspd = 0;
}
//Move horizontally
x += hspd;
   
//Verticle collisions ADD IN SCRIPT
if(place_meeting(x, y+vspd, obj_wall)){
    while(!place_meeting(x, y+sign(vspd), obj_wall)){
        y+= sign(vspd);
    }
    vspd = 0;
}
//Move vertically
y += vspd;
   
//Sprite control
if(hspd > 0){
   
    image_speed = 0.2;
    sprite_index = spr_player_right;
} else if(hspd < 0){
   
    image_speed = 0.2;
    sprite_index = spr_player_left;
}else{
   
    image_index = 0;
}

//Audio control
if(hspd > 0){
    audio_play_sound(aud_walking, 1, true);
} else if(hspd < 0){
    audio_play_sound(aud_walking, 1, true);
} else{
    audio_stop_sound(aud_walking);
}

if(hspd > 0){
    if !audio_is_playing(aud_walking)
    audio_play_sound(aud_walking, 1, true);
} else if(hspd < 0){
    if !audio_is_playing(aud_walking)   
    audio_play_sound(aud_walking, 1, true);
} else{
    audio_stop_sound(aud_walking);
    }
 

TheBroman90

Member
if(hspd > 0){
audio_play_sound(aud_walking, 1, true);
} else if(hspd < 0){
audio_play_sound(aud_walking, 1, true);
} else{
audio_stop_sound(aud_walking);
}
Remove this part and it should work.
 

NightFrost

Member
Is that just an error you made during copypaste, or is the audio code two times there at the end of the code? Your original followed by the code TheBroman90 supplied?
 

TheBroman90

Member
Code:
if !audio_is_playing(aud_walking)
    audio_play_sound(aud_walking, 1, true);
This part is exactly what it says. If the walk sound is not already playing, then play it. Then when it is playing, the code will skip this part so it won't machinegun loop.
The problem is that the code you removed played the sound again every step, even if it were already playing.
 
Top