Player bullets code for platformer

J

jgoldcaptain

Guest
Can someone share the code/events for creating bullets come out of players gun? It's a 2d platformer btw...
 
M

Maximus

Guest
You could place it in the step event of obj_player inside an if statement that checks the relevant key press

Code:
key_shoot = keyboard_check_pressed(vk_space);
if  (key_shoot) {
    with instance_create(x,y,obj_bullet) {
        image_xscale=other.image_xscale
        hspeed = 4 * image_xscale
    }
}
if you want to shoot towards the mouse
Code:
key_shoot = mouse_check_button_pressed (mb_left);
if (key_shoot)
{
    var bullet_start_dist = 10;
    var bullet_speed = 5;
    var dir = point_direction(x, y, mouse_x, mouse_y);
    var angle = degtorad( dir );
    var bullet = instance_create(x + bullet_start_dist * cos(angle), y - bullet_start_dist * sin(angle), obj_bullet);
    bullet.image_angle = dir;
    bullet.hspeed = bullet_speed * cos(angle);
    bullet.vspeed = -bullet_speed * sin(angle);
  
}
 
Last edited by a moderator:
J

jgoldcaptain

Guest
Thanks! This helped... my only issue is that the bullet needs to spawn one pixel downwards. How do I change that?
 
M

Maximus

Guest
just add a +1 to the y argument in the instance create function
instance_create(x ,y +1,obj_bullet)
 
Top