• 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 Shooting Code help (newbie)

R

Rosepike

Guest
Hello!

I'm only new to game maker, and when i say new, i mean I've been using the software for less than a month.
Anyway, I'm having problem with trying to get my player to shoot with the left mouse button, while also moving.
For some reason the left click button wasn't working for me so i replaced it with the "E" key instead. However, the player cannot move and shoot at the same time which is really annoying.

Heres my code;
///Movement
//Left
if (keyboard_check(ord("A"))) && (place_free(x - Speed,y))
{ sprite_index = s_player_right
image_speed = 0.2
x -= Speed
image_xscale = -1
}
//Right
if (keyboard_check(ord("D"))) && (place_free(x + Speed,y))
{ sprite_index = s_player_right
image_speed = 0.2
x += Speed
image_xscale = 1

}
//Idle
if (!keyboard_check(ord("A"))) && (!keyboard_check(ord("D")))
{ image_speed = 0
image_index = 1
}

//shoot Left
if (keyboard_check(ord("E"))) && (keyboard_check(ord("A"))) && (place_free(x - Speed,y)) {
sprite_index = spr_player_shoot;
image_speed = 0.2
x -= Speed
image_xscale = -1
global.facing = 1
instance_create(x,y,obj_shoot)
}


//Shoot Right
if (keyboard_check(ord("E"))) && (keyboard_check(ord("D"))) && (place_free(x + Speed,y)) {
sprite_index = spr_player_shoot;
image_speed = 0.2
x -= Speed
image_xscale = 1
global.facing = 0
instance_create(x,y,obj_shoot)
}
 

Perseus

Not Medusa
Forum Staff
Moderator
You don't need those extra if statements and actions for making the player shoot and move at the same time. The two things will be handled differently. So just check if the "E" key is being pressed and the other two if statements will take care of the movement.

Code:
if (keyboard_check_pressed(ord("E"))) {
    instance_create(x, y, obj_shoot);
}
Note how I used keyboard_check_pressed instead of keyboard_check. That is to avoid a wave of bullets when the said key is held down, since keyboard_check returns true as long as the key is held down, so instances would keep on getting created every step. On the other hand, keyboard_check_pressed returns true only once per key press, so only a single instance will be created until the key is pressed again. Hope that helps.
 
R

Rosepike

Guest
You don't need those extra if statements and actions for making the player shoot and move at the same time. The two things will be handled differently. So just check if the "E" key is being pressed and the other two if statements will take care of the movement.

Code:
if (keyboard_check_pressed(ord("E"))) {
    instance_create(x, y, obj_shoot);
}
Note how I used keyboard_check_pressed instead of keyboard_check. That is to avoid a wave of bullets when the said key is held down, since keyboard_check returns true as long as the key is held down, so instances would keep on getting created every step. On the other hand, keyboard_check_pressed returns true only once per key press, so only a single instance will be created until the key is pressed again. Hope that helps.
Thanks for the reply!

my problem would then be, how would i make it so that the bullets shoot where the player is facing?
i have tried this but its not working

if mouse_check_button_pressed(mb_left) || image_xscale = 1
 

Perseus

Not Medusa
Forum Staff
Moderator
Using the value of image_xscale, set hspeed of the newly created instance when the specified key or mouse button is pressed.

Code:
if (mouse_check_button_pressed(mb_left)) {
    var inst = instance_create(x, y, obj_shoot);
    inst.hspeed = 8 * image_xscale;
}
Code:
if (keyboard_check_pressed(ord("E"))) {
    var inst = instance_create(x, y, obj_shoot);
    inst.hspeed = 8 * image_xscale;
}
 
R

Rosepike

Guest
Using the value of image_xscale, set hspeed of the newly created instance when the specified key or mouse button is pressed.

Code:
if (mouse_check_button_pressed(mb_left)) {
    var inst = instance_create(x, y, obj_shoot);
    inst.hspeed = 8 * image_xscale;
}
Code:
if (keyboard_check_pressed(ord("E"))) {
    var inst = instance_create(x, y, obj_shoot);
    inst.hspeed = 8 * image_xscale;
}
Ahh! Thank you, that makes it so much easier, but then how would I have the sprite change to the sprite of the player shooting, sprite_index 't doesn't work
 

Perseus

Not Medusa
Forum Staff
Moderator
You can use an Alarm event for that. When the player shoots, set sprite_index to the shooting sprite and activate the alarm. When the alarm is activated, do not allow the player instance to change to a different sprite.

Code:
if (keyboard_check(ord("A")) && place_free(x - Speed, y)) {
    if (alarm[0] < 0) {
        sprite_index = spr_player_normal;
    }
    image_xscale = -1;
    x -= Speed;
}
if (keyboard_check(ord("D")) && place_free(x + Speed, y)) {
    if (alarm[0] < 0) {
        sprite_index = spr_player_normal;
    }
    image_xscale = 1;
    x += Speed;
}
if (mouse_check_button_pressed(mb_left)) {
    var inst = instance_create(x, y, obj_shoot);
    inst.hspeed = 8 * image_xscale;
    sprite_index = spr_player_shoot;
    alarm[0] = room_speed/2;
}
Set the alarm to the number of steps that you want the shooting sprite to remain for. For instance, in the code above, it remains for half a second. Note that for making the alarm count down, you must have a corresponding alarm event and an action in it, even if it's just a comment or an empty code action. Hope that helps.
 
C

CG_YT

Guest
Hi i am Trying to add some of this code into a game i am making, but all i really need help with is that i am making it move in all directions.
so i am asking if someone could help me with the code for the shooting portion of it and to check if the code is right since i am a newbie to gamemaker :)

Here Is my code:
Code:
///Movement + Shooting
//Left
if (keyboard_check(ord("A"))) && (place_free(x - Speed,y))
{ sprite_index = spr_soldier_left
image_speed = 0.1
x -= Speed
image_xscale = -1
}
//Right
if (keyboard_check(ord("D")) && place_free(x + Speed, y)) {
    if (alarm[0] < 0) {
        sprite_index = spr_soldier_right;
    }
    image_xscale = -1;
    x += Speed;
}
///Up
if (keyboard_check(ord("W")) && place_free(x, y + Speed)) {
    if (alarm[0] < 0) {
        sprite_index = spr_soldier_up;
    }
    image_xscale = -1;
    x += Speed;
}
//Down
if (keyboard_check(ord("S")) && place_free(x, y - Speed)) {
    if (alarm[0] < 0) {
        sprite_index = spr_soldier_down;
    }
    image_xscale = -1;
    x -= Speed;
}

//Idle
if (!keyboard_check(ord("A"))) && (!keyboard_check(ord("D"))) && (!keyboard_check(ord("W"))) && (!keyboard_check(ord("S")))
{ sprite_index = spr_soldier_up_idle
image_speed = 0
image_index = 1
}

//Shooting
if (mouse_check_button_pressed(mb_left)) {
    var inst = instance_create(x, y, obj_shoot);
    inst.hspeed = 8 * image_xscale;
    sprite_index = spr_soldier_shoot_right;
    alarm[0] = room_speed/2;
}
 
Top