CreateJS - 在两个容器中使用多个对象进行碰撞检测

时间:2017-08-13 18:48:49

标签: javascript collision-detection collision createjs

这是我用于碰撞检测的代码:

for(var k = 0; k < enemies.children.length; k++) { 

    if(bullet.x >= enemies.children[k].x + wingmanWidth || 
       bullet.x + sizeBullet <= enemies.children[k].x - wingmanWidth || 
       bullet.y >= enemies.children[k].y + wingmanHeight || 
       bullet.y + sizeBullet <= enemies.children[k].y) 
{
    //not
}else {
    // collsion detected
}

在敌人容器中的单个子弹对象和多个僚机对象之间检测到碰撞。现在我想检测子弹容器中的多个子弹与敌人容器中的多个僚机对象之间的碰撞。

1 个答案:

答案 0 :(得分:1)

通过添加另一个for循环来修复它,如Jozef建议的那样

for(var k = 0; k < enemies.children.length; k++) { 
  for(var j = 0; j < bulletsContainer.children.length; j++) {       
    if(bulletsContainer.children[j].x >= enemies.children[k].x + wingmanWidth || 
       bulletsContainer.children[j].x + sizeBullet <= enemies.children[k].x - wingmanWidth || 
       bulletsContainer.children[j].y >= enemies.children[k].y + wingmanHeight || 
       bulletsContainer.children[j].y + sizeBullet <= enemies.children[k].y) {

        //not
    } else {
        //yes
  }
}