shoot one bullet at the time

I

Isa

Guest
Hi guys,
Does anyone know the code to let a plane (it is a plane in my game) shoot bullets one after one?
Because now it is a long thing of bullets that is following my character
 

CloseRange

Member
how do you currently shoot? chances are you are either using keyboard_check mouse_check_button or the wrong event.
 
I

Isa

Guest
how do you currently shoot? chances are you are either using keyboard_check mouse_check_button or the wrong event.
Code event create for my plane:
var shootCoolDown;

shootCoolDown =0;
if(shootCoolDown <= 0){
bullet = instance_create (64,96, objKogel)
bullet.direction = move_towards_point(objHoofdPersoon.x, objHoofdPersoon.y,5);
bullet.image_angle = image_angle
bullet.speed = 15;

shootCoolDown= (0.5 * 5);
}

shootCoolDown -=1;

Code event alarm for my plane:
alarm[10] = 0.5 * room_speed;

Code event step for my plane:
instance_create(objVliegtuig.x,objVliegtuig.y, objKogel);
move_towards_point(objHoofdPersoon.x, objHoofdPersoon.y,5);

Code event create for bullets:
move_towards_point(objHoofdPersoon.x, objHoofdPersoon.y,5)

But my bullets are shooting to fast so I have no idea??
 

CloseRange

Member
in the step event you create a objKogel (i assume that's the bullet?)
that means every frame you create a new bullet.

Put hta tlin inside the alarm event and it should work out EXCEPT
alarm[10] is never called.
you need to give alarm[10] some inital value if you want it to ever get called.
So in the create event I'd just get rid of the shootCoolDown variable and do this:
Code:
alarm[10] = 1; // call the alarm right away
then in the alarm:
Code:
alarm[10] = 0.5 * room_speed; // .5 * room speed means half a second
var bullet = instance_create(objVliegtuig.x,objVliegtuig.y, objKogel);
      bullet.direction = move_towards_point(objHoofdPersoon.x, objHoofdPersoon.y,5); 
      bullet.image_angle = image_angle;
      bullet.speed = 15;
As for the bullet create event I'm not sure that is needed.
 
I

Isa

Guest
in the step event you create a objKogel (i assume that's the bullet?)
that means every frame you create a new bullet.

Put hta tlin inside the alarm event and it should work out EXCEPT
alarm[10] is never called.
you need to give alarm[10] some inital value if you want it to ever get called.
So in the create event I'd just get rid of the shootCoolDown variable and do this:
Code:
alarm[10] = 1; // call the alarm right away
then in the alarm:
Code:
alarm[10] = 0.5 * room_speed; // .5 * room speed means half a second
var bullet = instance_create(objVliegtuig.x,objVliegtuig.y, objKogel);
      bullet.direction = move_towards_point(objHoofdPersoon.x, objHoofdPersoon.y,5);
      bullet.image_angle = image_angle;
      bullet.speed = 15;
As for the bullet create event I'm not sure that is needed.
It still is kind of a snake of bullets that is following my character....
 

CloseRange

Member
did you remove the instance_create from the step event?
EDIT: in the alarm[10] event change it from .5 to something like 5.
That will be a 5 second delay instead of half a second so maybe that's all
 
I

Isa

Guest
in the step event you create a objKogel (i assume that's the bullet?)
that means every frame you create a new bullet.

Put hta tlin inside the alarm event and it should work out EXCEPT
alarm[10] is never called.
you need to give alarm[10] some inital value if you want it to ever get called.
So in the create event I'd just get rid of the shootCoolDown variable and do this:
Code:
alarm[10] = 1; // call the alarm right away
then in the alarm:
Code:
alarm[10] = 0.5 * room_speed; // .5 * room speed means half a second
var bullet = instance_create(objVliegtuig.x,objVliegtuig.y, objKogel);
      bullet.direction = move_towards_point(objHoofdPersoon.x, objHoofdPersoon.y,5);
      bullet.image_angle = image_angle;
      bullet.speed = 15;
As for the bullet create event I'm not sure that is needed.


I'm sorry but maybe I am stupid but was is "hta tlin" mean?:)
 
I

Isa

Guest
did you remove the instance_create from the step event?
No, and I get this error:
COMPILATION ERROR in code action
Error in code at line 3:
alarm[10] = 0.5 * room_speed;
^
at position 2: Cannot redeclare a builtin variable.
 

CloseRange

Member
I don't see why the error came up but I looked over the code more.
Code:
bullet.direction = move_towards_point(objHoofdPersoon.x, objHoofdPersoon.y,5);
this line that you had makes no sense.
direction is a variable that expects an angle (what angle to move at)
move_towards_point is a function that does 2 things:
1) sets your direction.
2) sets your speed.
it does NOT return a direction

I tested the code with no errors.
create event is still as i said.
alarm event should be:
Code:
alarm[10] = 5 * room_speed;
var bullet = instance_create(x, y, objKogel);
      bullet.direction = point_direction(bullet.x, bullet.y, mouse_x, mouse_y);
      bullet.image_angle = bullet.direction;
      bullet.speed = 15;
this will make the bullets go towards the mouse and be created at the current x and y but it should work.

Do not have any other code in this object or the bullet object.
Make sure this works then reintroduce making your plane move.
If you get the same error copy the exact error (what you showed me wasn't the full error) and the code how you copied it
 
I

Isa

Guest
I don't see why the error came up but I looked over the code more.
Code:
bullet.direction = move_towards_point(objHoofdPersoon.x, objHoofdPersoon.y,5);
this line that you had makes no sense.
direction is a variable that expects an angle (what angle to move at)
move_towards_point is a function that does 2 things:
1) sets your direction.
2) sets your speed.
it does NOT return a direction

I tested the code with no errors.
create event is still as i said.
alarm event should be:
Code:
alarm[10] = 5 * room_speed;
var bullet = instance_create(x, y, objKogel);
      bullet.direction = point_direction(bullet.x, bullet.y, mouse_x, mouse_y);
      bullet.image_angle = bullet.direction;
      bullet.speed = 15;
this will make the bullets go towards the mouse and be created at the current x and y but it should work.

Do not have any other code in this object or the bullet object.
Make sure this works then reintroduce making your plane move.
If you get the same error copy the exact error (what you showed me wasn't the full error) and the code how you copied it

My bullet are still to fast they don't get one after one
 
I

Isa

Guest
I don't see why the error came up but I looked over the code more.
Code:
bullet.direction = move_towards_point(objHoofdPersoon.x, objHoofdPersoon.y,5);
this line that you had makes no sense.
direction is a variable that expects an angle (what angle to move at)
move_towards_point is a function that does 2 things:
1) sets your direction.
2) sets your speed.
it does NOT return a direction

I tested the code with no errors.
create event is still as i said.
alarm event should be:
Code:
alarm[10] = 5 * room_speed;
var bullet = instance_create(x, y, objKogel);
      bullet.direction = point_direction(bullet.x, bullet.y, mouse_x, mouse_y);
      bullet.image_angle = bullet.direction;
      bullet.speed = 15;
this will make the bullets go towards the mouse and be created at the current x and y but it should work.

Do not have any other code in this object or the bullet object.
Make sure this works then reintroduce making your plane move.
If you get the same error copy the exact error (what you showed me wasn't the full error) and the code how you copied it
It doesn't have anything to do with the mouse.
The bullets have to shoot in the direction of my character
 

CloseRange

Member
It don't have anything to do with the mouse.
The bullets have to shoot in the direction of my character
sometimes, while programming and trying to figure out a problem. It's best to set the code to something you know works temporarily until you got something that does work, then you can make changes and add more complexity bit by bit. Once it breaks again you know where the problem lies.

Anyway I've created many shooting systems before and what I showed you is one of the simplest forms of one. If that isn't working then you have done something else.
You can link your project and I can look inside myself to find the problem or show all the code you currently have in the plane. all the code in the bullet.
If it's not a problem in either of those objects then there is another object somewhere that is creating bullets.
 
I

Isa

Guest
sometimes, while programming and trying to figure out a problem. It's best to set the code to something you know works temporarily until you got something that does work, then you can make changes and add more complexity bit by bit. Once it breaks again you know where the problem lies.

Anyway I've created many shooting systems before and what I showed you is one of the simplest forms of one. If that isn't working then you have done something else.
You can link your project and I can look inside myself to find the problem or show all the code you currently have in the plane. all the code in the bullet.
If it's not a problem in either of those objects then there is another object somewhere that is creating bullets.
How do I share it? haha I'm sorry I don't know how to do it??
 

johnwo

Member
You were on the right track with your original code.
Read the code below, learn from it; don't just mindlessly copypasta.

Player Create event:
Code:
shootCoolDown = 0; // Create the cooldown variable
Player Step event:
Code:
// We want to decrement the cooldown every step
shootCoolDown = max(0,shootCoolDown-1); // Same as saying if (shootCoolDown>0) then shootCoolDown--;
In the player "trigger event" (that is, the code that is called to shoot a bullet, e.g. Keybard-event, or even in the step event for continuous firing)
Code:
if(shootCoolDown <= 0) { // Check if the cooldown is in effect...
    bullet = instance_create (64,96, objKogel); // ...If it isn't, then create a bullet!
    bullet.direction = direction; // As stated in post #13, the bullet should have the same direction as the player.
    bullet.image_angle = bullet.direction; // Set image_angle to reflect direction
    bullet.speed = 15; // Set the speed

    shootCoolDown = (0.5 * room_speed); // Set the cooldown to room_speed/2
}
No need for alarms, albeit you could achieve the same by using alarms.
Setting the speed and direction of an instance will make that instance move with said speed and direction, no need for "move_towards_point" or any other code in the bullet object.

Happy coding!
 
Last edited:
I

Isa

Guest
You were on the right track with your original code.
Read the code below, learn from it; don't just mindlessly copypasta.

Player Create event:
Code:
shootCoolDown = 0; // Create the cooldown variable
Player Step event:
Code:
// We want to decrement the cooldown every step
shootCoolDown = max(0,shootCoolDown-1); // Same as saying if (shootCoolDown>0) then shootCoolDown--;
In the player "trigger event" (that is, the code that is called to shoot a bullet, e.g. Keybard-event, or even in the step event for continuous firing)
Code:
if(shootCoolDown <= 0) { // Check if the cooldown is in effect...
    bullet = instance_create (64,96, objKogel); // ...If it isn't, then create a bullet!
    bullet.direction = direction; // As stated in post #13, the bullet should have the same direction as the player.
    bullet.image_angle = bullet.direction; // Set image_angle to reflect direction
    bullet.speed = 15; // Set the speed

    shootCoolDown = (0.5 * room_speed); // Set the cooldown to room_speed/2
}
No need for alarms, albeit you could achieve the same by using alarms.
Setting the speed and direction of an instance will make that instance move with said speed and direction, no need for "move_towards_point" or any other code in the bullet object.

Happy coding!
Omg thank you so much!!!!!!
 
Top