GameMaker Trying to make double action shotgun

  • Thread starter RedPandaInTopHat
  • Start date
R

RedPandaInTopHat

Guest
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 by a moderator:

HayManMarc

Member
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. :/
 
K

Kawai_Oppai

Guest
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 by a moderator:

Yal

šŸ§ *penguin noises*
GMC Elder
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 */
}
 
Top