SOLVED How do I get my player space ship to shoot lasers automatically?

SlFinn

Member
Hi guys. I have successfully coded my spaceship for my space shooter to shoot lasers when I press the space bar. However I want the lasers to be shot from the space ship automatically and i can@t seem to figure it out. Could somebody please give me some example code to help me? I would really appreciate it.

This is the code i have done for the lasers so far in case you wanted to know:

In my oPlayer step event:

Code:
if keyboard_check_pressed(vk_space)
{
laser = instance_create_layer(x + 117, y - 1, "Instances",obj_laserbullet);
y -= speed;
laser.speed = 260;
laser.direction = -1;
}
In my obj_laserbullet Create Event:

Code:
speed = 260;
And in my obj_laserbullet Step Event:

Code:
y -= vspeed;
So i have my spaceship shooting via the space bar. But can't figure out how to make it automatic. Please help me if you can, and thanks for taking the time to read this! :)
 

Tornado

Member
You can for example set an alarm for this. Create for example Alarm 0 Event in your oPlayer object and put the creation of the bullet there.
Now you have to solve two "problems": calling that alarm for the first bullet and then calling it for further bullets.
For the first one you could write
Code:
alarm[0]=1;
under
Code:
if keyboard_check_pressed(vk_space)
so the first bullet will be fired via space key.

For subsequent bulets you can put directly in alarm 0 that alarm 0 sets itself again after some time. for example
Code:
// this is code in Alarm[0] of oPlayer object
bullet creation goes here
alarm[0] = room_speed;
will fire a bullet every second.

Then at some point you must stop shooting...so You could build a condition around alarm[0] = room_speed; so that alarm wont be set anymore after some condiotion is met. That way it will stop shooting.
 

SlFinn

Member
You can for example set an alarm for this. Create for example Alarm 0 Event in your oPlayer object and put the creation of the bullet there.
Now you have to solve two "problems": calling that alarm for the first bullet and then calling it for further bullets.
For the first one you could write
Code:
alarm[0]=1;
under
Code:
if keyboard_check_pressed(vk_space)
so the first bullet will be fired via space key.

For subsequent bulets you can put directly in alarm 0 that alarm 0 sets itself again after some time. for example
Code:
// this is code in Alarm[0] of oPlayer object
bullet creation goes here
alarm[0] = room_speed;
will fire a bullet every second.

Then at some point you must stop shooting...so You could build a condition around alarm[0] = room_speed; so that alarm wont be set anymore after some condiotion is met. That way it will stop shooting.
Is this right code for Alarm 0 event?

GML:
if keyboard_check_pressed(vk_space)
{
alarm[0] = 1;
laser = instance_create_layer(x + 132, y - 0.5, "Instances",obj_laserbullet);
y -= speed;
laser.speed = 260;
laser.direction = -1;
alarm[0] = room_speed;
}
 

Alice

Toolmaker of Bucuresti
Forum Staff
Moderator
You want to get rid of "keyboard_check_pressed(vk_space)" condition, or else you'll need to press space precisely every <room_speed> frames to keep shooting.
Also, alarm[0] = 1 line should be added to Create event rather than Alarm 0.

If you want to stop automatic shooting while the ship is still on-screen, you'll need to wrap "alarm[0] = room_speed" in another condition (e.g. "if (instance_exists(obj_WinScreen)) { ... }").
Resuming shooting might be trickier; generally, you need to add alarm[0] = 1 for the ship again, when the shoot-blocking condition isn't in effect anymore. Though maybe you won't need to stop then resume shooting, after all...?
 

Tornado

Member
Like Alice said
if keyboard_check_pressed(vk_space)
doesn't belong in the alarm 0.
Keep it like you had it before in the step event of the oPlayer object. Or remove it completely if you don't need key press.
It depends on how you want to start shooting.
 

SlFinn

Member
You want to get rid of "keyboard_check_pressed(vk_space)" condition, or else you'll need to press space precisely every <room_speed> frames to keep shooting.
Also, alarm[0] = 1 line should be added to Create event rather than Alarm 0.

If you want to stop automatic shooting while the ship is still on-screen, you'll need to wrap "alarm[0] = room_speed" in another condition (e.g. "if (instance_exists(obj_WinScreen)) { ... }").
Resuming shooting might be trickier; generally, you need to add alarm[0] = 1 for the ship again, when the shoot-blocking condition isn't in effect anymore. Though maybe you won't need to stop then resume shooting, after all...?
Thank you very much :) You're advice really helped me! The ships can shoot automatically now.! Ireally appreciate it!
 

SlFinn

Member
Like Alice said
if keyboard_check_pressed(vk_space)
doesn't belong in the alarm 0.
Keep it like you had it before in the step event of the oPlayer object. Or remove it completely if you don't need key press.
It depends on how you want to start shooting.
Thank you so much for your help :) My space ship is shooting automatically now!
 

SlFinn

Member
Though i still have the problem of my space ship moving slightly forward each time it shoots a laser bullet. Is there any way to fix that at all?
 
Top