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

Adding a delay of 2 sec before enemy changes it's sprite direction?

pixeltroid

Member
This very very basic code I have changes the enemy's sprite direction when the hero runs past him.

if (x<obj_hero.x) {
image_xscale = -1;
} else {
image_xscale = 1;
}

The sprite flip happens a instantly which is fine for some enemies. But I have some other enemies that I'd need to flip after a slight delay (basically give the hero 2 seconds to run away).

How should I go about implementing this?

Any help would be appreciated.
 

chamaeleon

Member
If you do want it time based you could us an alarm.
Step event
GML:
if (alarm[0] == -1 && sign(obj_hero.x - x) != image_xscale) {
    alarm[0] = 2 * game_get_speed(gamespeed_fps);
}
Alarm 0
GML:
if (sign(obj_hero.x - x) != image_xscale) {
    image_xscale *= -1;
}
 

pixeltroid

Member
If you do want it time based you could us an alarm.
Step event
GML:
if (alarm[0] == -1 && sign(obj_hero.x - x) != image_xscale) {
    alarm[0] = 2 * game_get_speed(gamespeed_fps);
}
Alarm 0
GML:
if (sign(obj_hero.x - x) != image_xscale) {
    image_xscale *= -1;
}
thanks. That works perfectly!

I'll implement this. But will use the distance delay for another type of enemy.
 
Top