GameMaker Bullets won't generate after 8 presses.

H

Hilo Takenaka

Guest
I am creating a metroidvania-style game where the player's main attack is a shooting attack.
What is meant to happen: as soon as the player presses the 'c' key, they shoot a bullet at the third frame of the animation.
The bullets are meant to be infinite, however the game only shoots 8 before no more are generated. It still enters the animation but doesn't generate the bullet.
I have also debugged the game to view if it recognizes the bullet creation, but it doesn't seem to do so after the eighth one.
Can anyone please help me with this issue?
Here is the relevant coding for the creation of the bullets.

Step Event:
if(sprite_index = s_player_shoot){
if (floor((image_index) = 3) && (fired = false)){
show_debug_message("whassad")
instance_create_layer(x, y, 0, o_bullet);
fired = true;

if (image_xscale = 1) {
o_bullet.direction = 0;
o_bullet.speed = 5;
} else {
o_bullet.direction = 180;
o_bullet.speed = -5;
}

}

}


Key up - C:
sprite_index = s_player_shoot;
image_speed = 0.5;
hsp = 0;
walksp = 0;
vsp = 10;
grv = 10;


Animation End:
if sprite_index = s_player_shoot{
sprite_index = s_player_ri;
walksp = 4;
grv = 1;
hsp = move * walksp;
vsp = vsp + grv;
fired = false;
}

s_player_right is the default sprite of the player
The bullet itself only has a collision event which destroys the bullet once it hits a solid object
 
I can immediately see a couple of problems with this code:
Code:
if(sprite_index = s_player_shoot){
if (floor((image_index) = 3) && (fired = false)){
show_debug_message("whassad")
instance_create_layer(x, y, 0, o_bullet);
fired = true;

if (image_xscale = 1) {
o_bullet.direction = 0;
o_bullet.speed = 5;
} else {
o_bullet.direction = 180;
o_bullet.speed = -5;
}

}

}
Where you have the floor() function, you are not flooring the correct thing. Your brackets are wrong so you are flooring the true/false value of whether image_index = 3.
Then where you are setting the values of direction and speed, you would be setting these values on a random instance of a bullet because you are using the o_bullet instead of an instance.
Try changing the above to be this:
Code:
if(sprite_index = s_player_shoot){
  if ((floor(image_index) >= 3) && (fired = false)){
    show_debug_message("whassad")
    var this_bullet = instance_create_layer(x, y, 0, o_bullet);
    fired = true;

    if (image_xscale = 1) {
      this_bullet.direction = 0;
      this_bullet.speed = 5;
    } else {
      this_bullet.direction = 180;
      this_bullet.speed = -5;
    }
  }
}
and see if that works any better
 
Also, if you're setting the direction, there's no need to alter the speed based on it. Setting the speed to a negative number while facing 180 will make it still shoot to the right.
 
H

Hilo Takenaka

Guest
Thank you to both of you! The shooting works like a charm now.
Personally I am quite unfamiliar with Gamemaker 2 so I wasn’t sure how to operate some of the newer functions.
I greatly appreciate the support from both of you.
 
Top