GMS 2 Trying to make double action shotgun

Discussion in 'Programming' started by RedPandaInTopHat, Oct 7, 2019.

  1. RedPandaInTopHat

    RedPandaInTopHat Member

    Joined:
    Oct 7, 2019
    Posts:
    1
    I tried to make a shotgun that after 2 shot pause for a few sec before able to shoot again, i've been at it the entire night but just could do it

    here's the Obj code but i don't think it will be relevant
    //Creation code
    firingdelay = 0

    //Begin step code
    firingdelay = firingdelay - 1

    if (mouse_check_button(mb_left)) and (firingdelay<0)
    {
    repeat(4)
    {
    firingdelay=30
    with (instance_create_layer(x,y,"Bullets",ShoBullet))
    {
    speed = 35
    direction = other.image_angle + random_range(-7,7)
    image_angle = direction
    }
    }
    }

    If anyone can help, i'll be grateful
     
    Last edited: Oct 7, 2019
  2. Alexx

    Alexx Member

    Joined:
    Jun 21, 2016
    Posts:
    436
    Show what code you currently have, someone is sure to give you some help.
     
  3. HayManMarc

    HayManMarc Member

    Joined:
    Jun 21, 2016
    Posts:
    910
    I'm not sure, but try...

    Create Event:
    Code:
    firingdelay = 0;
    
    Step Event:
    Code:
    if firingdelay > 0
        {
            firingdelay = firingdelay - 1;
        }
        else if (mouse_check_button(mb_left))
        {
            repeat(4)
                {
                    var shot = instance_create_layer(x, y, "Bullets", ShoBullet);
                    with (shot)
                        {
                            speed = 35;
                            direction = other.image_angle + random_range(-7, 7);
                            image_angle = direction;
                        }
                }
            firingdelay = 30;
        }
    
    What this is doing is:
    if the firingdelay is greater than zero, it subtracts one from it. Otherwise, it creates 4 instances of the object ShoBullet, then resets the firingdelay to 30.

    So every 30 steps, when the player is holding down the left mouse button, the gun will simultaneously shoot 4 bullets at slightly-offset, random angles toward the target.

    This is your code -- fixed up a little bit.

    If you want it to shoot like this twice, then make another longer pause, that takes another variable similar to your firingdelay variable.

    However, I have a feeling this isn't quite what you're after. :/
     
  4. Kawai_Oppai

    Kawai_Oppai Member

    Joined:
    Sep 26, 2019
    Posts:
    11
    I feel like using an alarm here would be useful and provide better control while keeping code easier to read.

    Basically after you shoot the gun, set an alarm for however long you want to wait, and only allow the gun to be shot when the alarm has completed.

    if canshoot
    {
    if keyboard_check_pressed(vk_space)
    {
    canshoot = false;
    alarm[0] = room_speed;
    instance_create_layer(x, y, "Bullets", obj_Bullet);
    }
    }

    When alarm completes it sets canshoot to true.

    This is the example from documentation on alarms.
     
    Last edited: Oct 8, 2019
  5. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,742
    Create event: set ammo to 2, canshoot to true.

    Shoot event: reduce ammo by 1, set "canshoot" to false. Check if ammo > 0. If it is, set alarm 0 to 5. If it isn't, set alarm 0 to 300 (This is 5 seconds if your game speed is 60 steps per second) and set ammo to 2 again.

    Alarm 0: set canshoot to true.

    Finally, go back to the shoot event and wrap the entire thing in an if statement:

    if(canshoot){
    /* all the existing code here, plus the new ammo stuff we just added */
    }
     

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