• 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!

GameMaker SOLVED / how to make my gun sound only playable when shoot animation finished?

D

Daniel White

Guest
When my player sprite shoots it plays the gun shot sound and the shoot animation but while the shoot animation is playing you can keep clicking to play more gun shot sounds. how do I make it so that you can't play that sound again until the animation has finished?

this is my step event sound at the bottom:

Code:
//Controls
if(state == "WALK" || state == "IDLE"){
    if keyboard_check(ord("D")) {
        x += walkSpeed;
        image_speed = walkSpeed / 3;
        sprite_index = spr_walk;
        facing = 1;
        state = "WALK";
    } else if keyboard_check(ord("A")) {
        x -= walkSpeed;
        image_speed = walkSpeed / 3;
        sprite_index = spr_walk;
        facing = -1;
        state = "WALK";
    } else {
        state = "IDLE";
    }
}

switch(state) {
    case "IDLE":
        image_speed = 0;
        sprite_index = spr_idle;
        walkSpeed = 0.6;
        if mouse_check_button(mb_right) {
            state = "AIM";
        }
        break;

    case "AIM":
        if sprite_index!=spr_aim {
            image_index = 0;
            sprite_index = spr_aim;
            image_speed = 1;
        }
        if(animation_end()){
            image_speed = 0;
        }
        else if(floor(image_index) == 0){
            image_speed = aimSpeed / 3;
        }
        if mouse_check_button_pressed(mb_left) {
            state = "SHOOT";
            sprite_index = spr_shoot;
            image_index = 0;
            image_speed = 1;
        }
        else if(!mouse_check_button(mb_right)){
            state = "IDLE";
        }
        break;

    case "SHOOT":
        //Once animation ends, return to AIM state last frame (4)
        if(animation_end()){
            sprite_index = spr_aim;
            image_index = 4;
            state = "AIM";
        }
        break;
        
    case "WALK":
        //Press SHIFT to run
        if keyboard_check(vk_shift) {
            walkSpeed = 1.1;
        }
        break;
}

//Sound effects

    //9mm gun sound
if (mouse_check_button_pressed(mb_left) and mouse_check_button(mb_right)){
        audio_play_sound(snd_9mm, 5, false)
}
 

samspade

Member
Move the audio_play_sound somewhere else. Right now you have it set up so that it plays when you're holding right and pressed left. This is disconnected from your actual state machine. You should put the sound somewhere in connection with the state machine. Where exactly depends on the project, but in a quick look over your code it seems like putting it when it changes state to shoot might work. Alternatively you could add it to the create event of the bullet in some cases or something else like that.
 
D

Daniel White

Guest
Try putting the line: audio_play_sound(snd_9mm, 5, false) in the case "SHOOT" in your switch statement.
i moved it into my SHOOT case but now the sound plays every frame of that shoot animation and not just the first frame

EDIT: fixed it ty all for help
 
Top