How would I get my enemy to flip its sprite

Rivo

7014
My enemy uses the A* path finding algorithm. (The built in version, i didn't write the whole thing) which it uses to chase the player or wander, and I would like to know how I would flip my enemies sprite according to what direction it faces (left or right)

Here is the code I tried that didn't work...

Code:
image_xscale = sign(direction)
Any help would be great thank you
 
Last edited:

RangerX

Member
each object is having built in variable for its X and Y orientation for drawing.

image_xscale and image_yscale

If you set them to 1, you get the correct orientation. If you want to flip or mirror, set the corresponding variable to -1
 

Lumenflower

Yellow Dog
I think the issue you might be having is in that direction is not defined when moving along a path. The instance simply takes steps along the path. A better method might be to say:

image_index = sign(x-xprevious)

Further, direction is a continuous variable between 0 and 360, rather than a boolean to describe movement left or right as you are treating it.
 

Conbeef

Member
image_angle = direction;?
Or
var Dir = round(direction/90)*90;

if Dir < 2 image_xscale = 1 else image_xscale = -1

Idk if this is what you're aiming for
 
S

Snail Man

Guest
Direction is a built in variable that refers to direction between 0-360 degrees. Try
Code:
image_xscale = sign(hspeed)
Referring to only the horizontal component of the object's movement
 
C

CoderJoe

Guest
Yeah I would use vspeed and hspeed like Snail Man said.
if hspeed > 1
image_xscale = -1;
if hspeed < 1
image_xscale = 1;

thats how I would do it.
 
J

jirrev

Guest
with a small alteration from an earlier code it might solve all problems at once:
Code:
if(x-xprevious != 0)
{
image_xscale = sign(x-xprevious);
}
This will flip the image around if nescesary but will only do so if there is a difference between x and xprevious
This will make sure your sprite doesn't dissapear.
 

Rivo

7014
also, my enemy only uses a speed variable since im using a pathfinder, i tried it with just speed and as I expected it didnt work. :/
 
B

BabyDukaCPH

Guest
I would try something like:

if(direction>89 && direction<271)
then
{
image_xscale=-1;
}
else
{
image_xscale=1;
}

Apply the same logic for Y axis.
This explains the logic of
Code:
direction
and the angle to refer to.
It works like a charm for me if put in the draw event. You can even scale the sprite if you change the value.
Ex.:
sprSize = .5;
if(direction>89 && direction<271)
then
{
image_xscale= - .sprSize;
}
else
{
image_xscale= .sprSize;
}
Sets variable sprSize (sprite size) and scales it (to half its size) according to the direction it's facing.
 
Top