6 Replies Latest reply on Apr 25, 2011 3:51 AM by LukeElChul

# AI: Turning Directions

This is a pretty complicated subject, but I'm hoping to make a two way direction moving AI "enemy." The movie clip has the character facing in one direction so when the player passes him, he stays in that direction, moving that way animated but moving towards the player on the opposite side.

I need something that rotates him horizontally so he'll be facing the player and looks as though he's moving towards him.

code so far: (bunny and fox are just inputs from a past game) (all code is in frame 1)

function moveBunnies() {
// loop through all objects looking for bunnies
for(var i=objects.length-1;i>=0;i--) {
if (objects[i].type == "bunny") {

// move only bunnies on screen
if (Math.abs(objects[i].x-foxPos.x) < 275) {

// move toward fox
if (foxPos.x < objects[i].x) {
var dx = -bunnySpeed;
} else if (foxPos.x > objects[i].x) {
var dx = bunnySpeed;
}

// determine bounds for this bunny
bunnyBounds = determineBounds(objects[i]);

// move only as far as bounds
if ((dx < 0) and (bunnyBounds.left > Math.abs(dx))) {
objects[i].x += dx;
} else if ((dx > 0) and (bunnyBounds.right > Math.abs(dx))) {
objects[i].x += dx;
}

// see whether bunny is close enough to fox
if (distance(_root["object "+i],fox) < 30) {
setScore(-5);
fox.gotoAndPlay(25);
}
if (getTimer() > nextHitTime) {
nextHitTime = getTimer()+1000;
}
}
}
}
}

So far I have a code that turns the player's character left or right by keys but I'd like to know how to do this with code on AI.

Also if anyone knows, There will be times where the player will be be above the AI so when he's under the player, he'll just stop instead of vibrating rapidly or (if it works) moving in both directions rapidly.

Please help too. I have to have this game finished in less than 3 days.

• ###### 1. Re: AI: Turning Directions

You can use something like this:

clip._xscale *=-1;

That will flip it horizontally. You will need to figure out when you need to do that.

• ###### 2. Re: AI: Turning Directions

} else if (playerPos.x > objects[i].x) {
var dx = enemySpeed;

I found this bit of code that inserts "if player is to the left of enemy, enemy moves at enemy speed (right is -enemySpeed)"

but unfortunetly, your code given doesn't have any effect.

enemy._rotation = 180;
_root.enemy_rotation = 180;

• ###### 3. Re: AI: Turning Directions

The code I gave will flip a movieclip horizontally. If it doesn't then you have something else going on.

So do you want to rotate it or flip it?

• ###### 4. Re: AI: Turning Directions

I want to fli pit horizontally so it's facing the other direction. Again, the code didn't quite work.

• ###### 5. Re: AI: Turning Directions

So HOW does it not work?

#### here's a killer script you can use for your problam

speed = 2.5;

}

onClipEvent (enterFrame) {

if (_root.Walk._x<_root.enemy._x-35) {

_x -= speed;

this._xscale = 100;

}

if (_root.Walk._x>_root.enemy._x+35) {

_x += speed;

this._xscale = -100;

}

}