# Making one set of MC's move toward another

**URL:** <https://forum.kirupa.com/t/making-one-set-of-mcs-move-toward-another/259955>\
**Category:** programming\
**Created:** [May 10, 2008, 10:09pm UTC](https://forum.kirupa.com/t/making-one-set-of-mcs-move-toward-another/259955 "2008-05-10T22:09:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zuperxtreme](https://avatars.discourse-cdn.com/v4/letter/z/e9a140/32.png) [@zuperxtreme](https://forum.kirupa.com/u/zuperxtreme)\
**Post date:** [May 10, 2008, 10:09pm UTC](https://forum.kirupa.com/t/making-one-set-of-mcs-move-toward-another/259955/1 "2008-05-10T22:09:36Z")

</div>

I need the MC’s on the left, to choose(randomly if possible) a target to the right and move towards it.

My code is:

```auto
//ALL IN ARRAY MOVE TOWARDS ONE ENEMY
var iArray:Array = [];
var kArray:Array = [];
//object 1 array
for (i=0; i<20; i++) {
    newI = "i_"+i;
    iArray.push(newI);
    _root.attachMovie("i", newI, _root.getNextHighestDepth());
    _root[newI]._x = 100+Math.round(Math.random()*30);
    _root[newI]._y = Math.round(Math.random()*300);
}
//object 2 array
for (k=0; k<5; k++) {
    newK = "k_"+k;
    kArray.push(newK);
    _root.attachMovie("k", newK, _root.getNextHighestDepth());
    _root[newK]._x = 400+Math.round(Math.random()*30);
    _root[newK]._y = Math.round(Math.random()*300);
}
//loops,target, move and hitT
for (l=0; l != kArray.length; l++) {
    for (m=iArray.length; m != 0; m--) {
        targetL = eval(kArray[l]);
        targetM = eval(iArray[Math.round(Math.random()*iArray.length)]);
        if (targetM == undefined) {
            targetM = eval(iArray[1]);
        }
        targetL.onEnterFrame = function() {
            //trace(targetM);
            if (this._x<targetM._x) {
                this._x++;
            } else if (this._x>targetM._x) {
                this._x--;
            }
            if (this._y<targetM._y) {
                this._y++;
            } else if (this._y>targetM._y) {
                this._y--;
            }
            if (this.hitTest(targetM)) {
                trace("HIT"+targetM);
                trace(this+" vs "+targetM);
            }
        };
    }
}

```

[http://img236.imageshack.us/my.php?image=zombiedefatkphaselh8.swf](http://img236.imageshack.us/my.php?image=zombiedefatkphaselh8.swf)

As you can see they all go towards one, when each of them should go individually to another individual(or not), just not all to the same.

Hope someone understands…

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [May 10, 2008, 11:23pm UTC](https://forum.kirupa.com/t/making-one-set-of-mcs-move-toward-another/259955/2 "2008-05-10T23:23:04Z")

</div>

```auto
if (targetM == undefined) {
            targetM = eval(iArray[1]);
}

Should that be [1] or [l]

```

Perhaps that’s why they’re targetting the same man?

---

<div class="post-metadata">

**Author:** ![zuperxtreme](https://avatars.discourse-cdn.com/v4/letter/z/e9a140/32.png) [@zuperxtreme](https://forum.kirupa.com/u/zuperxtreme)\
**Post date:** [May 10, 2008, 11:45pm UTC](https://forum.kirupa.com/t/making-one-set-of-mcs-move-toward-another/259955/3 "2008-05-10T23:45:28Z")

</div>

I got it to work. I inserted MC’s in the Arrays differently and gave each object in the array a personal target, heh. Like so, in case someone else ever needs this:

```auto
//ALL IN ARRAY MOVE TOWARDS ONE ENEMY
var iArray:Array = [];
var kArray:Array = [];
//object 1 array
for (i=0; i<5; i++) {
    var newI = _root.attachMovie("i", i, _root.getNextHighestDepth());
    iArray.push(newI);
    iArray*._x = 100+Math.round(Math.random()*30);
    iArray*._y = Math.round(Math.random()*300);
    trace(iArray)
}
//object 2 array
for (k=0; k<5; k++) {
    var newK = _root.attachMovie("k", k, _root.getNextHighestDepth());
    kArray.push(newK);
    
    kArray[k]._x = 400+Math.round(Math.random()*30);
    kArray[k]._y = Math.round(Math.random()*300);
    kArray[k].toFollow = Math.round(Math.random()*iArray.length);
    kArray[k].onEnterFrame = function() {
        if (this._x<iArray[this.toFollow]._x) {
            this._x += 2;
        } else if (this._x>iArray[this.toFollow]._x) {
            this._x -= 2;
        }
        if (this._y<iArray[this.toFollow]._y) {
            this._y += 2;
        } else if (this._y>iArray[this.toFollow]._y) {
            this._y -= 2;
        }
    };
}

```

Cheers!
