GML move enemy left and right

tbrides

Member
hello,
I'm trying to get an enemy object to move just left and right

here is my code in the Step event:

if(x<=room_width-100){

x+=flySpeed; //flySpeed is a variable in the Create event

}else{

x-=flySpeed;
}

But it's sticking to the right side of the room and not moving back
 

TheouAegis

Member
Yeah because as soon as x is greater than room_width-10, it moves back flyspeed, so then it's less than room_width-100. So then it moves right flySpeed. But then it's greater than room_width-100, so it flies left flySpeed. Now it's less than room_width-100 again, so it flies right...

if median(x,100,room_width-100) != x
flySpeed = -flySpeed;
x += flySpeed;
 
Top