-->

as3 Enemies in an array hit testing each other?

2019-08-21 02:34发布

问题:

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?

回答1:

To get rid of the error, i should be smaller than (enemyArray.length - 1), but the logic will still be faulty because it doesn't check collisions between all enemies. To check collisions between all your enemies you should do something like:

for (var i:int = 0; i < enemyArray.length - 1; i++) {
   var enemy1 = enemyArray[i];

   for(var j:int = i+1; j< enemyArray.length; j++) {
      var enemy2 = enemyArray[j];
      if(enemy1.hitTestObject(enemy2)){
            //do something on collision
      }
   }
}

If you want something performant, especially if you have a lot of enemies, you should compare the coordinates of the enemies and see if they overlap. You can read more about collision for multiple objects here: http://www.emanueleferonato.com/2008/06/07/managing-multiple-balls-collisions-with-flash-as3-version/