# SOLVEDCreating flying enemies that fly back and forth/up and down

#### Aaron Walton

##### Member
I'm trying to create a basic flying enemy that will just fly back and forth or up and down, depending on how I set each instance's variable definition (so one will go up and down, another back and forth). Basically Super Mario's flying Koopas. I've technically gotten it working, but I want to be able to easily change each instance's speed, distance traveled, and direction traveled. As it's working right now, I'm not able to tell them how far to travel, but how long to travel (in frames) each direction. Since it's measured in frames rather than pixels, if I make it faster it will travel for a longer distance before switching directions. Is there a way to tell it to go x number of pixels before turning around instead of x number of frames?

My creation code is (actually it's in variable definitions to be able to access easily later):
GML:
``````hsp = 0;
vsp = 0;
flying = 1;
flyingdistance = 60 * 1;
flyingspeed = 2;
vertical = false;
horizontal = false;``````
And the step event is:
GML:
``````if vertical
{
if flying > 0 && flying < (flyingdistance)
{
flying++;
if flying >= (flyingdistance) flying = -1;
}
if flying < 0 && flying > -(flyingdistance)
{
flying--;
if flying <= -(flyingdistance) flying = 1;
}
vsp = (sign(flying) * flyingspeed);
}

if horizontal
{
if flying > 0 && flying < (flyingdistance)
{
flying++;
if flying >= (flyingdistance) flying = -1;
}
if flying < 0 && flying > -(flyingdistance)
{
flying--;
if flying <= -(flyingdistance) flying = 1;
}
hsp = (sign(flying) * flyingspeed);
}

x += hsp;
y += vsp;``````

#### Mk.2

##### Member
Something like:

Code:
``````//Create Event
initX = x;
destX = x + 100;``````
Then change direction once the enemy's x coordinate has passed or is equal to initX or destX, depending on which direction they're currently moving.

Last edited:

#### Aaron Walton

##### Member
Something like:

Code:
``````//Create Event
initX = x;
destX = x + 100;``````
Then change direction once the enemy's x coordinate has passed or is equal to initX or destX, depending on which direction they're currently moving.
Oh that worked perfectly! Thanks!

• Mk.2