阵列中的AS3敌人击中试探对方?阵列、敌人、对方

2023-09-08 14:58:47 作者:不懂我就不要说我变了

我进展缓慢与我的测试游戏,我已经得到的地步,我可以通过一个阵列中添加敌人的舞台上,他们跟随玩家和对球员没有问题旋转。我的下一个任务就是给每个敌人的碰撞检测与对方,否则,他们最终都只是彼此完全重叠,这就是不太现实的。

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.

下面是我的code到目前为止只是对于敌人:

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;
                }

因此​​,这里也正是我坚持。在enemy.hitTestObject部分,我原来只是有敌人hitTesting敌人,而是使敌方打测试本身并完全搞砸了移动和旋转。所以,我想,而不是我们所拥有的上面,打在阵列上的下一个现场测试与敌人实例的每个敌人的情况下了,它的工作原理与大多数的敌人,但不是全部,并产生一个错误,因为数组中的最后一个实例将努力的HitTest不存在一个实例,并且在表现一个巨大的下降。

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?

推荐答案

要摆脱错误的,我应该比小(enemyArray.length - 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
      }
   }
}

如果你想要的东西高性能,特别是如果你有很多的敌人,你应该比较敌人的坐标,并看看他们是否重叠。你可以阅读更多有关在这里多物体碰撞:http://www.emanueleferonato.com/2008/06/07/managing-multiple-balls-collisions-with-flash-as3-version/

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/