• 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!

How do you create bullets that shoot in the direction you are facing(maze game/RPG)

8

82499

Guest
I am running Gamemaker 8.1 Lite. My direction variable for my main character won't work and so setting the bullet direction as character direction won't work. I don't know how else to make the bullet move in the direction of character.
 
8

82499

Guest
///scr_move_state
right_key = keyboard_check(vk_right);
left_key = keyboard_check(vk_left);
up_key = keyboard_check(vk_up);
down_key = keyboard_check(vk_down);
img_spd = .2;
//Move right
if (right_key) {
obj_player.x += spd;
sprite_index = spr_player_right;
image_speed = img_spd;
}
//Move left
if (left_key) {
obj_player.x -= spd;
sprite_index = spr_player_left;
image_speed = img_spd;
}
//Move up
if (up_key) {
obj_player.y -= spd;
sprite_index = spr_player_up;
image_speed = img_spd;
}
//Move down
if (down_key) {
obj_player.y += spd;
sprite_index = spr_player_down;
image_speed = img_spd;
}
//Stop animating
if (!right_key and !left_key and !up_key and !down_key) {
image_speed = 0;
image_index = 0;
}
 

TheouAegis

Member
Ah. Then you'll have to use a switch.

Code:
switch sprite_index {
    case spr_player_right: create bullet going right; break; <<don't forget the break
    case spr_player_up: create bullet going up; break;
    case spr_player_left: create bullet going left; break;
    case spr_player_down: create bullet going down; break;
}
Sneaky edit: I forgot you were on 8.1, I was trying to sneak in a Studio shortcut. lol
 
8

82499

Guest
can you explain what this means so I can use it for future cases?
Thanks
 
8

82499

Guest
and also, I dont know exactly what you mean by create bullet going right etc
 

TheouAegis

Member
A switch is just a cleaner way of saying if-else-if-else-if-else-if-else. They have the format
switch <expression> {
case <value> :
<statement>;
break;
case <value> :
<statement>;
break;
}

and also, I dont know exactly what you mean by create bullet going right etc
"Create bullet going right" means just that. Create a bullet and set its direction/speed so that it moves to the right. Ditto with the other directions. So for example
Code:
with instance_create(x,y,obj_bullet) {

    switch other.sprite_index {
       case spr_player_right: direction = 0; break;
       case spr_player_up: direction = 90; break;
       case spr_player_left: direction = 180; break;
       case spr_player_down: direction = 270; break;
    }

    speed = 4;
}
 
8

82499

Guest
upload_2019-8-25_21-24-35.pngupload_2019-8-25_21-25-2.pngupload_2019-8-25_21-25-28.png



regardless of player direction, my variable direction just doesnt work
 
8

82499

Guest
I figured out the direction problem, just now when i press fire, the game just ccrashes. I'm thinking of doing it where the direction of bullet = player direction. Set teh speed to x for the create event. However, when i fire a new bullet in another direction with other bullets still existing, they all switch directions to the new player direction. Can i do something where if the bullet direction isnt = to player direction, then don't change direction???
Not sure
 

TheouAegis

Member
I did not tell you to use player's direction. I said use the player's sprite. You never set the player's direction, so you can't use it.

The code i posted will create a bullet moving where the player is facing. Your problem is you either said "with obj_bullet direction = 180" or you said "obj_bullet.direction = 180", which are both incorrect.
 
Top