wow... for as long as I've been making games, Ive always had one problem... overlapping enemies...

My AI goes like this.

Code:
if (enemy.x > hero.x){
enemy.scaleX = -1;
}

if (enemy.x < hero.x){
enemy.scaleX = 1;
}

if (Math.abs(enemy.x-hero.x) > 20){
enemy.velocity.x = enemy.scaleX*30;
}

if (Math.abs(enemy.x-hero.x) < 20){
enemy.velocity.x = 0;
}
every enemy does this, and there is another script that stops the enemy when it gets within 20 pixels of the "hero" and makes it attack. If you stand still for too long, the enemies will end up in the same position, overlapping each other.

All the enemies are in an array together, so I thought I could do something like this.

Code:
for (var i=0; i<enemies.length; i++){

     enemies = enemies.sortOn(Math.abs(enemies[i].x-(element ("hero").x)));

     if (Math.abs(enemies[i].y-element ("hero").y) < i*15){
     enemies[i].velocity = 0;
     }
}
this is supposed to place the enemy that is closest (or furthest, not sure) as the first item in the array, the enemies are not sorted however... is their any alternative to keeping the enemies from covering each other up???