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

Discussion in 'Programming' started by Daniel White, Mar 24, 2019.

  1. Daniel White

    Daniel White Member

    Joined:
    Mar 23, 2019
    Posts:
    15
    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)
    }
    
     
  2. Tony Brice

    Tony Brice Member

    Joined:
    Feb 4, 2017
    Posts:
    68
    Try putting the line: audio_play_sound(snd_9mm, 5, false) in the case "SHOOT" in your switch statement.
     
  3. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,039
    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.
     
  4. Daniel White

    Daniel White Member

    Joined:
    Mar 23, 2019
    Posts:
    15
    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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice