Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.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
Javascript 两个阵列的p5冲突检测不起作用_Javascript_Arrays_For Loop_Collision Detection_P5.js - Fatal编程技术网

Javascript 两个阵列的p5冲突检测不起作用

Javascript 两个阵列的p5冲突检测不起作用,javascript,arrays,for-loop,collision-detection,p5.js,Javascript,Arrays,For Loop,Collision Detection,P5.js,早上好 我正在尝试在来自不同阵列的两个对象之间进行碰撞检测。 我尝试使用两个for循环,但这不起作用,因为seedArray()未定义 这是我的代码: for (let i in seedArray) { for (let j in monsterArray) { if ( seedArray[i].x > monsterArray[j].x && seedArray[i].x + seedArray[

早上好

我正在尝试在来自不同阵列的两个对象之间进行碰撞检测。 我尝试使用两个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(让i进入种子数组){
for(让j在数组中){
如果(
种子数组[i].x>monsterArray[j].x&&
种子数组[i].x+种子数组[i].radius<
怪物阵列[j].x+怪物宽度&&
种子数组[i].y>monsterArray[j].y&&
种子数组[i]。y+种子数组[i]。半径<怪物数组[j]。y+怪物高度
) {
gameEnd();
重置();
}
}
}
有没有办法让它发挥作用


提前谢谢

那么您使用的是语句中的for..in,我认为在这个场景中您不需要它

for…in语句遍历 由字符串设置关键帧的对象(忽略由符号设置关键帧的对象), 包括继承的可枚举属性

您需要的是语句中..的

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();
        }
    }
}
for(让种子数组的种子){
对于(让怪物中的怪物阵列){
如果(
种子.x>怪物.x&&
种子.x+种子.radius<
怪物。x+怪物宽度&&
种子.y>怪物.y&&
种子.y+种子.radius<怪物.y+怪物高度
) {
gameEnd();
重置();
}
}
}