我有一个点击事件,它检查数组中的对象是否应该被删除(isSquashed),当它为真时,我们从数组列表中删除该对象,但是当出现这种情况时,我需要跳出循环或递减i值,基本上我想跳出for循环,在拼接数组对象后用newArray列表长度再次调用gameloop
我尝试了两种方式
向后迭代 for 循环
给出中断声明(获得非法中断声明)但它仍然没有发生在我身上所以有人可以帮我解决这个问题并让我知道我如何解决这个问题,
maingameloop = function(antsArray) {
//inititialization
// antsArray[i].draw();
// antsArray[i].checkifSmashed();
//gameloop
if (this.isplaying) {
console.log(this.score);
for (let i = 0; i < antsArray.length; i++) {
let gameloop = setInterval(() => {
antsArray[i].move();
antsArray[i].update(antsArray);
if (antsArray[i].isSquashed) {
this.score++;
antsArray.splice(i, 1);
// i--;
clearInterval(gameloop);
// this.isplaying = false ;
}
},
this.FRAME_RATE,
);
}
} else {
//gameover
// this.maingameloop(antsArray);
}
}
从数组中删除对象时得到的 o/p 是:
Uncaught TypeError: Cannot read property 'move' of undefined
德玛西亚99
撒科打诨
四季花海
相关分类