# Follow Closest With Multiple Movieclips

**URL:** https://forum.kirupa.com/t/follow-closest-with-multiple-movieclips/323348
**Category:** flash
**Created:** [June 26, 2011, 11:44pm UTC](https://forum.kirupa.com/t/follow-closest-with-multiple-movieclips/323348 "2011-06-26T23:44:35Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![DanDaMan](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@DanDaMan](https://forum.kirupa.com/u/DanDaMan)
#### Post date: [June 26, 2011, 11:44pm UTC](https://forum.kirupa.com/t/follow-closest-with-multiple-movieclips/323348/1 "2011-06-26T23:44:35Z")

</div>

Hello, I’m trying to make it so that there are a bunch of instances (let’s say 20) that move around about the stage. There are also 5 other instances (let’s call them followers) that move and rotate towards the nearest of the 20 moving instances. I achieved this with this code in an ENTER\_FRAME event:

```php

for (var i:int=0; i<_root.eArray[0].length; i++) {
	var obj=_root.eArray[0]*;
	var distX:Number=this.x-obj.x;
	var distY:Number=this.y-obj.y;
	var dist:Number=(distX*distX+distY*distY);
	if (dist<max) {
		max=dist;
		c=_root.eArray[0]*;
	}
}
max=Number.MAX_VALUE;
angle=Math.atan2(c.y-this.y,c.x-this.x);
speedX=Math.cos(angle)*2;
speedY=Math.sin(angle)*2;
rotation=angle/(Math.PI/180);
this.x+=speedX;
this.y+=speedY;

```

This code is placed in the class file for the follower objects. The problem is that I don’t want more than one follower following the same movieclip. I was thinking that after the for loop I would remove c (the closest movieclip and the one it is following) from \_root.eArray[0] and put it in \_root.eArray[1] so that the other followers would not count it when searching for the closest. But I couldn’t figure out the best way to go about doing it. I’d really appreciate any help
