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

Flipping Directions for Projectiles When Shooting

I been trying to figure this out for about an hour, but i am trying to program my character shooting projectiles. Well, i got the character to shoot projectiles, but it is only shooting to the right when I move my character to the right. When I move my character to the left, the projectiles still go to the right.

I was wondering how I can code it to where when I move left the projectiles will go in that direction. I know have to put "image_xscale" in the code, but I don't know how I code it. Here is what I have so far:

Code:
// Shoot Projectile

if keyboard_check(ord('A')) && (canShoot) 
{
instance_create(x+16,y,obj_bullet);
canShoot = false
alarm[0] = shotCounter;
}
Here is the code for flipping the animations of my sprite

Code:
//Animations

if (hsp == 0)

{
    sprite_index = spr_flare
    image_speed = 0.1;       

}
else if keyboard_check(vk_right)
{
    sprite_index = spr_flarerun
    image_xscale = 1
    image_speed = .1;
    
}else if keyboard_check(vk_left)
{
    sprite_index = spr_flarerun
    image_xscale = -1
    image_speed = .1;   
}
 
D

Diveyoc

Guest
I think within your obj_bullet create event, just add:
direction = obj_player.direction;

Providing your player object is named obj_player, otherwise change the name accordingly.
 
I think within your obj_bullet create event, just add:
direction = obj_player.direction;

Providing your player object is named obj_player, otherwise change the name accordingly.
When I coded that and when I press the button when playing the game it will give me an error.
 

matharoo

manualman
GameMaker Dev.
Code:
if keyboard_check(ord('A')) && (canShoot)
{
bullet = instance_create(x+(16*image_xscale),y,obj_bullet);
with (bullet){
    image_xscale = other.image_xscale;
    speed = bullet_speed * image_xscale;
}
canShoot = false
alarm[0] = shotCounter;
}
Basically you just need to change its image_xscale to that of the player and multiply its speed with its image_xscale. So, say the image_xscale is 1 (moving right), speed will be, say, 5 * 1, moving to the right. But if the image_xscale is -1 (moving left), speed will become 5 * -1, moving to the left. (Because of the negative speed)

Here I've used the speed variable but feel free to use whatever you are using currently and in whatever event/object. You just need to understand the concept.
 
A

Ampersand

Guest
Why give the bullet a direction? If there's no gravity or change in momentum, I would just give the bullet an hspeed of "bullet_speed * player.image_xscale" as matharoo stated. You may be overthinking this a bit.
 
Top