Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Libgdx 我有一个爆炸阵列和一个僵尸阵列,如何有效地检测碰撞?_Libgdx_Game Physics - Fatal编程技术网

Libgdx 我有一个爆炸阵列和一个僵尸阵列,如何有效地检测碰撞?

Libgdx 我有一个爆炸阵列和一个僵尸阵列,如何有效地检测碰撞?,libgdx,game-physics,Libgdx,Game Physics,我有一组爆炸和一组僵尸,但我正在努力找到一种有效检测碰撞并移除碰撞的爆炸和僵尸的方法。有什么建议吗 Iterator<Rectangle> blastIter = blasts.iterator(); while(blastIter.hasNext()) { Rectangle blast = blastIter.next(); blast.x += 200 * Gdx.graphics.getDeltaTime();

我有一组爆炸和一组僵尸,但我正在努力找到一种有效检测碰撞并移除碰撞的爆炸和僵尸的方法。有什么建议吗

    Iterator<Rectangle> blastIter = blasts.iterator();

    while(blastIter.hasNext()) {
        Rectangle blast = blastIter.next();
        blast.x += 200 * Gdx.graphics.getDeltaTime();
        if(blast.x + 16 > 800) blastIter.remove();
    }

    Iterator<Rectangle> zombieIter = zombies.iterator();

    while(zombieIter.hasNext()) {
        Rectangle zombie = zombieIter.next();
        zombie.x -= 150 * Gdx.graphics.getDeltaTime();
        //if(zombie.overlaps(blast)) zombieIter.remove();
    }
Iterator blastIter=blasts.Iterator();
while(blastIter.hasNext()){
矩形blast=blastIter.next();
blast.x+=200*Gdx.graphics.getDeltaTime();
如果(blast.x+16>800)blastIter.remove();
}
迭代器zombieIter=zombies.Iterator();
while(zombieIter.hasNext()){
矩形僵尸=zombieter.next();
zombie.x-=150*Gdx.graphics.getDeltaTime();
//if(zombie.overlaps(blast))zombieIter.remove();
}

创建嵌套循环,就像在那里一样,但一个循环在另一个循环中(这在另一个循环中并不重要),因此您将拥有这两个列表中的所有objecst组合。

您需要将另一个迭代器放在另一个迭代器中,请尝试以下操作:

Iterator<Rectangle> zombieIter = zombies.iterator();

while(zombieIter.hasNext()) {

    Rectangle zombie = zombieIter.next();
    zombie.x -= 150 * Gdx.graphics.getDeltaTime();

    Iterator<Rectangle> blastIter = blasts.iterator();

    while(blastIter.hasNext()) {

        Rectangle blast = blastIter.next();
        blast.x += 200 * Gdx.graphics.getDeltaTime();

        if(blast.x + 16 > 800) blastIter.remove();
        if(zombie.overlaps(blast)) zombieIter.remove();

    } 
}
Iterator zombieIter=zombies.Iterator();
while(zombieIter.hasNext()){
矩形僵尸=zombieter.next();
zombie.x-=150*Gdx.graphics.getDeltaTime();
迭代器blastIter=blasts.Iterator();
while(blastIter.hasNext()){
矩形blast=blastIter.next();
blast.x+=200*Gdx.graphics.getDeltaTime();
如果(blast.x+16>800)blastIter.remove();
if(zombie.overlaps(blast))zombieIter.remove();
} 
}

谢谢!我最终使用了类似的东西。我唯一不同的做法是在循环外声明
迭代器blastIter
,但在循环内初始化它,并将blast.x增量移出循环,否则如果有多个blast,blast的速度会越来越快:)