How would I get my enemy to flip its sprite

Discussion in 'Programming' started by Rivo, Jun 22, 2016.

1. Rivo7014

Joined:
Jun 22, 2016
Posts:
205
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: Jun 22, 2016
2. RangerXMember

Joined:
Jun 20, 2016
Posts:
2,587
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

3. Rivo7014

Joined:
Jun 22, 2016
Posts:
205
Yeah sorry, i accidentally put index isntead. What I meant was xscale, which doesnt work

4. Druid TCYellow Dog

Joined:
Jun 20, 2016
Posts:
40
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.

Rivo likes this.
5. RangerXMember

Joined:
Jun 20, 2016
Posts:
2,587
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.

BabyDukaCPH and Rivo like this.
6. ConbeefMember

Joined:
Jun 21, 2016
Posts:
108
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

7. Snail ManMember

Joined:
Jun 20, 2016
Posts:
282
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

8. CoderJoeMember

Joined:
Jun 22, 2016
Posts:
166
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.

9. jirrevGuest

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 likes this.
10. Rivo7014

Joined:
Jun 22, 2016
Posts:
205
I appreciate the help guys! I've got it sorted now. Thank you all very much.

11. Rivo7014

Joined:
Jun 22, 2016
Posts:
205
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. :/

12. BabyDukaCPHMember

Joined:
Feb 8, 2017
Posts:
18
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.