• 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!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code Problem with enemy bullet directions

C

Curry

Guest
I'm creating a platformer game. I want the enemies to constantly shoot in the direction they're facing, but I'm having a problem: the direction of the enemy bullets are all in sync. (In other words, one of the enemies correctly shoots in whichever direction it is facing, but all the other enemies shoot in that same direction, regardless of the direction they're facing).

I think the reason for this is because 'obj_enemy' means all instances of obj_enemy, but I want individual instances of obj_enemy to shoot in their own directions. How can I fix this issue? Here's some of the code I'm using:

(obj_enemy step event)
//create bullets
instance_create_depth(x, y, 0, obj_bullet);

(obj_bullet step event)
//fire direction
if obj_enemy.image_xscale > 0 { //facing right
x += hsp; //bullet moves right
} else if obj_enemy.image_xscale < 0 { //facing left
x -= hsp; //bullet moves left
}
 

FrostyCat

Redemption Seeker
Make a distinction between objects and instances, and put the instance ID returned by instance_create_depth() to actual use.
NEVER access a single instance by object ID if multiple instances of the object exist. This includes attempts to reference or set object.variable (which is inconsistent across exports) and using with (object) to apply actions to it (this encompasses all instances of the object instead of just the one you want). Verbally, "Dog's colour" makes sense with one dog, but not with multiple dogs.
Created or copied instance. instance_create() (GMS 1.x and legacy) / instance_create_layer() (GMS 2.x) and instance_copy() return the ID of the created or copied instance. NEVER use instance_nearest() to establish this relationship --- something else could be closer by.
Code:
// Creating the bullet
var inst_bullet = instance_create_depth(x, y, 0, obj_bullet);
inst_bullet.hdir = sign(image_xscale);
Code:
// Bullet's Step event
x += hsp*hdir;
Also, post code-related questions like this in the Programming section next time. The Technical Support sections are for general IDE and setup issues.
 
Top