I'm progressing slowly with my test game, and I've gotten to the point where I can add enemies to the stage through an array, they follow the player and rotate towards the player with no problem. My next task is to give each enemy collision detection with each other, otherwise they all eventually just completely overlap each other and that's not very realistic.
Here's my code so far just regarding the enemies:
public function initEnemy():void
{
enemyArray = new Array();
for (var i = 0; i < 2; i++)
{
var enemy:Enemy = new Enemy((Math.random() *500 + 20), (Math.random() * 200 + 50));
stage.addChild(enemy);
enemyArray.push(enemy);
}
}
public function enemyBehavior():void
{
var enemyRotation:int;
for (var i:int = 0; i < enemyArray.length; i++)
{
var enemy = enemyArray[i];
var dx:int = enemy.x - player.x;
var dy:int = enemy.y - player.y;
var dr:int = dx * dx + dy * dy;
var dr2:int = Math.sqrt(dr);
enemy.x -= enemySpeed * dx / dr2;
enemy.y -= enemySpeed * dy / dr2;
enemyRotation = -(Math.atan2(enemy.x - player.x, enemy.y - player.y) * 180 / Math.PI);
trace(enemyRotation);
enemy.rotation = enemyRotation;
if (enemy.hitTestObject(enemyArray[i+1]))
{
enemy.x -= -enemySpeed;
enemy.y -= -enemySpeed;
}
So here's where I'm stuck. At the enemy.hitTestObject portion, I originally just had enemy hitTesting enemy, but that causes the enemy to hit test itself and completely screwed up the movement and rotation. So I tried instead what we have above, hit testing each enemy instance with the enemy instance next in the next spot on the array, and it works with most of the enemies but not all, and produces an error because the last instance in the array will be trying to hitTest an instance that doesn't exist, and there is a huge decrease in performance.
Can someone give me some sort of direction on a better way to hit test these objects from the array that is more performance friendly?