Firing Multiple Projectiles at Once

I started programming an enemy in which he shoot missiles at the player. It is similar to the trucks from Metal Slug 3. Its at 2:15


I got everything programmed and it works as it should. However, it only shoots one missile at a time. I was wondering how I can go about programming this so it can shoot a barrage of them(like 10-15). Also, I want the enemy to stop firing the missiles after that certain amount and start the process all over again.

GML:
switch(state)
{
case crabtank.idle:
    hsp = 0;
    timer += 1
    if (timer >= max_missle_timer)
    {
    timer = 0
    state = crabtank.anticipation;
    }
break;
    case crabtank.anticipation:
    timer += 1
    if timer >= max_missle_timer
    {
    timer = 0
    instance_create_layer(x,y,"Enemy",obj_projectile)
    vsp = -40
    state = crabtank.missle_up
    }
    break;
    case crabtank.missle_up:
    
    {
    state = crabtank.missle_wait;   
    }
    break;
    case crabtank.missle_wait:
    timer += 1
    num = irandom_range(1,100)
    if (timer >= missle_wait_timer)  && num < 5
    {
    timer = 0   
    xp = irandom_range(12,1325)
    yp = 100
    abc = instance_create_layer(xp,yp,"Enemy",obj_trance)
    abc.speed = 8
    abc.direction = 270
    state = crabtank.missles
    }
    break;
    case crabtank.missles:
    timer += 1
    if timer >= missle_wait_timer
    {
    timer = 0
    state = crabtank.idle   
    }
    break;
}
 

Bentley

Member
Pseudo Code
Create 10-15 bullets at once
GML:
if (canShoot)
{
    repeat (irandom_range(10, 15))
    {
        instance_create_layer(x, y, layer, oBullet);
    }
    canShoot = false;
}
You would put this code in your state machine.
As you want that to happen again after an amount of time, set an alarm and in it set canShoot to true (or just use your timer variable).
 
Pseudo Code
Create 10-15 bullets at once
GML:
if (canShoot)
{
    repeat (irandom_range(10, 15))
    {
        instance_create_layer(x, y, layer, oBullet);
    }
    canShoot = false;
}
You would put this code in your state machine.
As you want that to happen again after an amount of time, set an alarm and in it set canShoot to true (or just use your timer variable).

I actually got it to work, hank you so much. Sorry for the late reply, but I was actually getting frustrated with this, but it works. However, it fires the missiles all at once, but I want them fire at separate times like the missles from MS 3 video .

GML:
function scr_tank_misslewait(){
    timer += 1
    num = irandom_range(1,100)
    if (timer >= missle_wait_timer)  && num < 5
    {
        repeat (irandom_range(10, 15))
        {
    timer = 0
    xp = irandom_range(12,1325)
    yp = 100
    abc = instance_create_layer(xp,yp,"Enemy",obj_trance)
    abc.speed = 8
    abc.direction = 270
    state = crabtank.missles
        }

    }
}
 

Bentley

Member
Something like this might work.
GML:
// Create
missileTotal = irandom_range(10, 15);
event_perform(ev_alarm, 0);

// Alarm[0]
instance_create_layer(x, y, layer, oMissile); 
missileTotal--;
if (missileTotal <= 0) exit;
alarm[1] = irandom_range(1, 8); // In 1-8 frames, refire a missile

// Alarm[1]
event_perform(ev_alarm, 0);
You can offset the X position of the missiles, like in the video, by offsetting the x coordinate by a random amount in instance_create_layer
 
Last edited:
Something like this might work.
GML:
// Create
missileTotal = irandom_range(10, 15);
event_perform(ev_alarm, 0);

// Alarm[0]
instance_create_layer(x, y, layer, oMissile);
missileTotal--;
if (missileTotal <= 0) exit;
alarm[1] = irandom_range(1, 8); // In 1-8 frames, refire a missile

// Alarm[1]
event_perform(ev_alarm, 0);
You can offset the X position of the missiles, like in the video, by offsetting the x coordinate by a random amount in instance_create_layer
I was wondering how I can put this in to a state machine, unless I put it into the alarm event.
 

AgentLym

Member
Alternatively, you can avoid switching to your "missile_up" state until several missiles have been fired. For example, say you wanted to fire a missile every 4 frames (ie, 15 missiles per second in a 60fps game), and you wanted to fire 2 seconds worth of missiles (ie, 30 total). At the very start of the missile_fire state, set a timer to 4*30 (time between missiles * number of missiles). Then, use the modulo operator to fire the missiles at the right time while the timer counts down. Example:

GML:
//When settings fire_missile state
state = STATE.fire_missiles;
missile_timer = 4 * 30;

//Later on, in the state machine...
case STATE.fire_missiles:
    if(missile_timer mod 4 == 0) //fire a missile every 4 frames
    missile_timer--;
   
    if(missile_timer <= 0) state = STATE.missile_up;
    break;
 
Top