I have a click event which checks if the object in the array should be removed(isSquashed) and when it's true we remove the object from the list of array but when that condition comes I either need to break out of the loop or decrement the i
value, basically I want to come out of the for loop and call the gameloop again with the newArray list length after splicing the array object
I tried both ways
- iterating the for loop backward
giving the break statement(getting illegal break Statement) but it's still not happening for me so could someone help me out with this and let me know how I could possibly fix this,
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); } }
the o/p I'm getting when I remove the object from the array is:
Uncaught TypeError: Cannot read property 'move' of undefined