• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Do animation before shooting and prevent spam key

F

Fizz

Guest
So i have a problem with the animation.
when i hold key_shoot for 3 sec the player animation will play, and when i release the key, obj_ball will out.
but even i not hold key_shoot for 3 sec the obj_ball will out only by released key for 1 sec
this problem make my player can spam a bullet only in 1 sec

and how to make "only shoot when player hold the key and release" *charging
and how to make my player shoot to left (default was right). my sprite change to left and right but the obj_ball still shoot to right.

*animation before shoot is like charging energy

// Animation before shoot
key_shoot = keyboard_check(ord("Z"));

if sprite_index = spr_player1_stay && key_shoot
{
sprite_index = spr_player1_shoot;
}
else
{
sprite_index = spr_player1_stay;

// Shooting
shootdelay = shootdelay - 1;
if ((keyboard_check_released(ord("Z"))) && (shootdelay < 0))
{
shootdelay = 5;
with (instance_create_layer(x,y,"Ball",obj_ball))
{
speed = 10;
}
}

i already try using pressed and released in key_shoot but that only prevent animation to play

Thanks!

#Need Help
 

PlayerOne

Member
What I don't see is any code for the charge being made - I.e. holding the button for shootdelay counter to increase. I see the z key but not the actual holding of said key. Maybe I'm just blind this morning.

Code:
//CREATE:
shootdelay=0;


//STEP:
var _speed=0.5; // <<< Speed of the charge.

if alarm[0]=-1
{
   if keyboard_check(ord("Z"))
   {
  
      if sprite_index>=image_number-1 && image_speed>0
      {
      image_speed=0
      shootdelay+=_speed;
      }

   if chargeshot>=3
   {
   image_speed=1;
   sprite_index=fire_shot;
   
    image_index=0; // Reset index.
    
    if sprite_index>=image_number-2
    {
      with (instance_create_layer(x,y,"Ball",obj_ball))
      {
      speed = 10;
      }
      alarm[0]=10; // <<< Cool down
    }
   }
}

//ALARM 0
//No code needs to be added, the actual event however needs to be added to the object.
Use an alarm for the cooldown. Thats best for any kind of shooting delay.
 
F

Fizz

Guest
What I don't see is any code for the charge being made - I.e. holding the button for shootdelay counter to increase. I see the z key but not the actual holding of said key. Maybe I'm just blind this morning.

Code:
//CREATE:
shootdelay=0;


//STEP:
var _speed=0.5; // <<< Speed of the charge.

if alarm[0]=-1
{
   if keyboard_check(ord("Z"))
   {
 
      if sprite_index>=image_number-1 && image_speed>0
      {
      image_speed=0
      shootdelay+=_speed;
      }

   if chargeshot>=3
   {
   image_speed=1;
   sprite_index=fire_shot;
  
    image_index=0; // Reset index.
   
    if sprite_index>=image_number-2
    {
      with (instance_create_layer(x,y,"Ball",obj_ball))
      {
      speed = 10;
      }
      alarm[0]=10; // <<< Cool down
    }
   }
}

//ALARM 0
//No code needs to be added, the actual event however needs to be added to the object.
Use an alarm for the cooldown. Thats best for any kind of shooting delay.
Thanks for your reply, but sorry it was not work. and im still confused about chargeshot there, and fire_shot is spr_player1_shoot?
do i need to delete my key_shoot in Move Key Statement?
and i think the Z key was not work properly to shot the ball

The animation play but the Ball doesnt out. im so glad if you can help me again
 

PlayerOne

Member
Thanks for your reply, but sorry it was not work. and im still confused about chargeshot there, and fire_shot is spr_player1_shoot?
do i need to delete my key_shoot in Move Key Statement?
and i think the Z key was not work properly to shot the ball

The animation play but the Ball doesnt out. im so glad if you can help me again
Did you use my code or yours? I made a mistake in it when I wrote this my code. I rewrote yours and still added the alarm as that is the most effective way for bullet cool down.

Code:
if (alarm[0]==-1 && keyboard_check(ord("Z")))
{

//Change Sprite
sprite_index = spr_player1_stay;

//Charge Shot
chargeshot++; // Increase amount before firing.

if (keyboard_check_released(ord("Z")) && chargeshot<3)
{
sprite_index = spr_player1_shoot;
chargeshot=0;
}
else if ((chargeshot >= 3))
   {
      with (instance_create_layer(x,y,"Ball",obj_ball))
      {
      speed = 10;
      }
      alarm[0]=10; // Shoot delay
   }


}

//ALARM 0
//No code needs to be added, the actual event however needs to be added to the object.
 
Top