# 2D AI movement [Solved]

C

#### Choppa

##### Guest
Hello everyone! I am new around these parts, but I am currently making a 2D game. The problem that I am facing at the moment is with the AI mechanics. I have no clue how to flip the sprite that is moving.

Right now as it goes I have a sprite AI that moves left and right. Once it hits a wall it is suppose to turn around and walk the other way. Currently I have the sprite hit a wall and turn to go the other way, but I have no idea how to flip the sprite itself so it looks the other way. Can anyone help me out, or direct me in the path I need to go? Thank you for your time.

Code:
``````///Initialize Variables

dir = -1; //Horizontal Variable
//Movement variables
movespeed = 3;
grav = 0.35;
hsp = 0;
vsp = 0;``````
Code:
``````hsp = dir * movespeed;
vsp += grav;

//Horizontal Collision
if (place_meeting(x+hsp,y,obj_grassblock1))
{
while(!place_meeting(x+sign(hsp),y,obj_grassblock1))
{
x += sign(hsp);
}
hsp = 0;
dir *= -1;
}
if (place_meeting(x+hsp,y,obj_grassblock2))
{
while(!place_meeting(x+sign(hsp),y,obj_grassblock2))
{
x += sign(hsp);
}
hsp = 0;

dir *= -1;
}
if (place_meeting(x+hsp,y,obj_aibar))
{
while(!place_meeting(x+sign(hsp),y,obj_aibar))
{
x += sign(hsp);
}
hsp = 0;
dir *= -1;
}
x += hsp;
//Vertical Collision
if (place_meeting(x,y+vsp,obj_grassblock1))
{
while(!place_meeting(x,y+vsp,obj_grassblock1))
{
y += sign(vsp);
}
vsp = 0;
}
if (place_meeting(x,y+vsp,obj_grassblock2))
{
while(!place_meeting(x,y+vsp,obj_grassblock2))
{
y += sign(vsp);
}
vsp = 0;
dir *= -1;
}
y += vsp;``````

#### the_dude_abides

##### Member
If I remember correctly: image_xscale set to -1 reverses the image.

So you just need to do something to link that to the direction it's heading.

If your sprite was drawn facing right, then when it's heading right image_xscale =1.

If it's heading left then image_xscale = -1

(but my memory of this is a bit fuzzy.....)

#### TheouAegis

##### Member
image_xscale = dir;

C

Thanks!