I first tried a forEach loop to delete items from an array, but later learned that you shouldn't do this. So I tried to make one with a normal for loop. This is to cycle through an array containing bullets, and delete them when they go outside the game area.
for (i = 0; i < playerBullets.length; i++) {
console.log(playerBullets[i].x);
console.log(playerBullets[i]);
if (playerBullets[i].x > 800 || playerBullets[i].x < 0 || playerBullets[i].y > 600 || playerBullets[i].y < 0 ) {
playerBullets.splice(i);
}
}
The console correctly shows [i] in full, and brings up a list of all contents of the array. However, the [i].x console log only displays one value, rather than the "x" value of each object in the array.
Then as soon as the first bullet goes out of bounds, all bullets disappear. Most frustrating, and highly ineffective for killing zombies.
I have also tried looping backwards through the loop, which seems to be the recommended way but it tells me that i is undefined.
Any ideas? I feel like I'm making a very simple error, because I'm using the same structure as the code on tutorial sites, so it "should work".
Thank you!