你如何检测两个阵列之间的冲突?

时间:2022-06-24 23:04:25

For future refrence this was the final code

为了将来的参考,这是最终的代码

for each (var bullet:Bullet in bulletList)
{
    for each (var zombie:Zombie in zombieList)
    {
        if (zombie.hitTestObject(bullet))
        {
            stage.removeChild(bullet);
            zombie.zombieShot(50);
        }

    }
}   

Original Question Below

 for each (var bullet:Bullet in bulletList)
{
    if (zombieList.length > 0)
    {
        if (zombieList[h].hitTestObject(bullet))
        {
            bulletList[i].deleteBullet();
            zombieList[h].zombieShot(50);
        }
    }
}

This is the code I have but it only detects the first zombie I spawn in, any help is appreciated.

这是我的代码,但它只检测我产生的第一个僵尸,任何帮助表示赞赏。

if (countMePls<10)
{
    countMePls++;
    var zombie:Zombie = new Zombie(stage,Math.random() * stage.width,Math.random()*stage.height);
    zombie.addEventListener(Event.REMOVED_FROM_STAGE,zombieRemoved,false,0,true);
    zombieList.push(zombie);
    stage.addChild(zombie);
}

and then...

function shootBullet():void
{
var bullet:Bullet = new Bullet(stage,Main.player.x,Main.player.y,Main.player.rotation - 90);
bullet.addEventListener(Event.REMOVED_FROM_STAGE,bulletRemoved,false,0,true);
bulletList.push(bullet);
stage.addChildAt(bullet,1);
}

this last bit is in Bullet.as

最后一点是在Bullet.as中

public function deleteBullet():void
    {
        this.parent.removeChild(this)
    }

1 个解决方案

#1


1  

I think your issue comes from some basic confusion about for and for each. With for each you have no index variable, each iteration yields a new instance of the type in the collection which is referred to by the name you declare in the loop. In this case that is;

我认为你的问题来自于对于每个人的一些基本困惑。对于每个都没有索引变量,每次迭代都会在集合中生成一个类型的新实例,该实例由您在循环中声明的名称引用。在这种情况下是;

  foreach (var bullet in bulletsList)
  {
      // do something with bullet
  }

You probably actually want a nested loop, something that checks every bullet against each zombie to see if there was a hit, that would actually look like this;

你可能真的想要一个嵌套循环,它可以检查每个僵尸对每个僵尸是否有命中,实际上看起来像这样;

  foreach (var bullt in bulletsList)
  {
       foreach (var zombie in zombiesList)
       {
             if (zombie.hitTestObject(bullet))
             {
                     bulletList.Remove(bullet);
                     zombie.zombieShot(50);
             }
       }
  }

In your code you have the foreach loop giving you the current bullet object but then you never reference it within the loop, that doesn't make sense. This may not be exactly what you want but hopefully it will get you moving in the right direction. If you want to use those indexers then you need something like;

在你的代码中你有foreach循环给你当前的子弹对象但是你永远不会在循环中引用它,这是没有意义的。这可能不是你想要的,但希望它会让你朝着正确的方向前进。如果你想使用那些索引器,那么你需要类似的东西;

  for (int i = 0; i < bulletsList.Length; i++)
  {
       for (int h = 0; h < zombiesList.Length; h++)
       {
            // do stuff with bulletsList[i] and zombiesList[h]
       }
  }

Note: this was originally tagged as C# and the code I've posted uses C# syntax. The explanation I've provided most likely applies either way, the OP's code doesn't really makes sense in any language I know of and the reasons are the same.

注意:这最初标记为C#,我发布的代码使用C#语法。我提供的解释很可能适用于任何一种方式,OP的代码在我所知的任何语言中都没有意义,原因是相同的。

#1


1  

I think your issue comes from some basic confusion about for and for each. With for each you have no index variable, each iteration yields a new instance of the type in the collection which is referred to by the name you declare in the loop. In this case that is;

我认为你的问题来自于对于每个人的一些基本困惑。对于每个都没有索引变量,每次迭代都会在集合中生成一个类型的新实例,该实例由您在循环中声明的名称引用。在这种情况下是;

  foreach (var bullet in bulletsList)
  {
      // do something with bullet
  }

You probably actually want a nested loop, something that checks every bullet against each zombie to see if there was a hit, that would actually look like this;

你可能真的想要一个嵌套循环,它可以检查每个僵尸对每个僵尸是否有命中,实际上看起来像这样;

  foreach (var bullt in bulletsList)
  {
       foreach (var zombie in zombiesList)
       {
             if (zombie.hitTestObject(bullet))
             {
                     bulletList.Remove(bullet);
                     zombie.zombieShot(50);
             }
       }
  }

In your code you have the foreach loop giving you the current bullet object but then you never reference it within the loop, that doesn't make sense. This may not be exactly what you want but hopefully it will get you moving in the right direction. If you want to use those indexers then you need something like;

在你的代码中你有foreach循环给你当前的子弹对象但是你永远不会在循环中引用它,这是没有意义的。这可能不是你想要的,但希望它会让你朝着正确的方向前进。如果你想使用那些索引器,那么你需要类似的东西;

  for (int i = 0; i < bulletsList.Length; i++)
  {
       for (int h = 0; h < zombiesList.Length; h++)
       {
            // do stuff with bulletsList[i] and zombiesList[h]
       }
  }

Note: this was originally tagged as C# and the code I've posted uses C# syntax. The explanation I've provided most likely applies either way, the OP's code doesn't really makes sense in any language I know of and the reasons are the same.

注意:这最初标记为C#,我发布的代码使用C#语法。我提供的解释很可能适用于任何一种方式,OP的代码在我所知的任何语言中都没有意义,原因是相同的。