# [F8] Following Enemies

• 11-07-2008, 07:13 PM
InfiniteSource
[F8] Following Enemies
Hello flash kitters :)

Could someone help me with a code that I've been searching for a while? This code that I'm looking for is supposed like this...

There is a hero/character and hes surrounded by zombies!!!!
But then the zombies that are surrounding him aren't coming to attack. This is because I'm looking for a code which lets the zombie to follow the hero/characters path. Also something to make the zombie look realistic! Like smooth rotation when it's turning. It's supposed to be real lifeish :)

• 11-08-2008, 01:13 AM
pseudobot
Just check where the hero's _x and _y is, and then move untill the _x and _y are equal. Won't take too long to do with a bit of thinking.
• 11-08-2008, 10:44 AM
InfiniteSource
Is there a code that I could get? I'm not very smart on figuring out those things yet. :( I'm only 13
• 11-08-2008, 03:17 PM
brukman
Don't worry, I'm 15! :D I can help!
This is one way to do it:

For each zombie you could put the code in their enterFrame thing:

Code:

```onClipEvent(enterFrame) //or "onEnterFrame=function()" depending on where you put the code {         this.xdist=_root.hero._x-this._x; //the distance between the _x positions of the two         this.ydist=this._y-_root.hero._y; //the distance between the _y positions of the two         this.totaldist=Math.sqrt((xdist*xdist)+(ydist*ydist)); //total distance between the two using Pythagorean Theorem         this.xadd=this.totaldist/this.xdist; //number to add to the _x of the zombie         this.yadd=this.totaldist/this.ydist; //number to add to the _y of the zombie         this._x+=this.xadd; //add it to the _x         this._y+=this.yadd; //add it to the _y }```
You need to replace "hero" with the name of the hero or character in your game.

Other than trigonometry, I think that's the only way to make them move at a constant speed towards the enemy.

Btw- I would strongly recommend that you create the zombies in code rather than on the stage, however, which is where the side note "onEnterFrame=function()" comes from. Use a "for" loop in the main timeline to create the zombies.

VERY IMPORTANT: In order for this second method to work, you need to find the zombie movieClip in the library, right click on it, choose linkage, and give it an identifyer of "zombie", for use in code

This is how it would be done (in the main timeline code):
Code:

```for(i=1; i<=number; i++) //replace "number" with the number of zombies you wish to create {         thezombie=attachMovie("zombie","zombie"+i,_root.getNextHighestDepth(),{_x:Math.random()*550,_y:Math.random()*400}); //creates the zombie(s) and sends them to a random position on the stage         thezombie.onEnterFrame=function()         {                 this.xdist=_root.hero._x-this._x; //the distance between the _x positions of the two                 this.ydist=this._y-_root.hero._y; //the distance between the _y positions of the two                 this.totaldist=Math.sqrt((xdist*xdist)+(ydist*ydist)); //total distance between the two using Pythagorean Theorem                 this.xadd=this.totaldist/this.xdist; //number to add to the _x of the zombie                 this.yadd=this.totaldist/this.ydist; //number to add to the _y of the zombie                 this._x+=this.xadd; //add it to the _x                 this._y+=this.yadd; //add it to the _y         } }```
Voila.
• 11-08-2008, 04:51 PM
InfiniteSource
Yay! Go little kids! :) But then the code that I'm trying which you just provided above... I'm not sure how it was supposed to work but I followed your instructions but it didn't work for me :(
• 11-08-2008, 07:26 PM
pseudobot
^And what didn't EXACTLY work?
• 11-08-2008, 08:05 PM
InfiniteSource
Well what didn't work well for me was that the considered... ZOmbiE is multipied by 400 or 550 covering everything in the screenie! :(
• 11-08-2008, 08:11 PM
pseudobot
well, just sete the variable
"number"
to something like 10.
PHP Code:

``` number = 10;  ```
• 11-09-2008, 01:36 AM
brukman
In the line of code where is says:

Code:

`this.totaldist=Math.sqrt((xdist*xdist)+(ydist*ydist));`
there should be a "this." before every xdist and ydist, my bad.
• 11-09-2008, 01:25 PM
InfiniteSource
Okay it works now :) Thanks everyone
• 11-09-2008, 01:25 PM
InfiniteSource
I just played around with the codes so it worked! :)