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

player x enemy

VovoNauta

Member
HI!, I'm a beginner and would like to know how to make the enemy change the sprite (walk on opposite sides) when I accompany the player?
The player can change sides easily (image.xscale=-1), but the enemy can't, he doesn't collide with anything and when he reverses the direction I can't change the image.
sorry for the translation(google)
 

chamaeleon

Member
Are you looking for something different or more complex than
Enemy step
GML:
if (sign(obj_player.x - x) != 0) {
    image_xscale = sign(obj_player.x - x);
}
The intent for this code is to have the enemy "look" in the direction of the player. I am uncertain if this is what you are actually asking for, but maybe it's a starting point.
 

VovoNauta

Member
tks chamaeleon..!

I got a palliative solution:

if collision_circle(x, y,120, obj_enemy, false, true)
{
obj_enemy.image_xscale=1
}
else image_index = 0;

(event create of player)

Tks!
 

FrostyCat

Redemption Seeker
tks chamaeleon..!

I got a palliative solution:

if collision_circle(x, y,120, obj_enemy, false, true)
{
obj_enemy.image_xscale=1
}
else image_index = 0;

(event create of player)

Tks!
That solution is more defective than it is palliative. Do you know the difference between an object and an instance?
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.
Collision-checking functions: instance_place() and instance_position() are the instance-ID-oriented analogues of place_meeting() and position_meeting(). Functions that start with collision_ but don't end in _list all return instance IDs. Save that instance ID into a variable, then use that as the subject to work with. DO remember to check that it is not noone before acting on it. DO NOT refer to this instance as other.
GML:
var inst_enemy = instance_place(x, y, obj_enemy);
if (inst_enemy != noone) {
    inst_enemy.hp -= 10;
}
 
Top