两个阵列的 p5 碰撞检测不起作用

p5 collision detection with two arrays does not work

早上好,

我正在尝试对来自不同阵列的两个对象进行碰撞检测。 我尝试使用两个 for 循环,但这不起作用,因为 seedArray() 未定义。

这是我的代码:

for (let i in seedArray) {
    for (let j in monsterArray) {
        if (
            seedArray[i].x > monsterArray[j].x &&
            seedArray[i].x + seedArray[i].radius <
            monsterArray[j].x + monsterWidth &&
            seedArray[i].y > monsterArray[j].y &&
            seedArray[i].y + seedArray[i].radius < monsterArray[j].y + monsterHeight
        ) {
            gameEnd();
            reset();
        }
    }
}

有没有办法让它工作?

提前致谢!

所以您正在使用 for..in 语句,我认为您在这种情况下不需要它。

The for...in statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties. find out more

你需要的是for..of语句:

for (let seed of seedArray) {
    for (let monster of monsterArray) {
        if (
            seed.x > monster.x &&
            seed.x + seed.radius <
            monster.x + monsterWidth &&
            seed.y > monster.y &&
            seed.y + seed.radius < monster.y + monsterHeight
        ) {
            gameEnd();
            reset();
        }
    }
}