I'm creating a game where waves of enemies are coming to me and I have to kill them.
So far, I'm doing great: it took me hours to come up with this nested loop to check for collision detection and when I ran it the game was running great... but for a few kills only. Sometimes I manage to play it even for a few minutes until it crashes, sometimes I shoot twice and it crashes, and I always get this error:
"Uncaught TypeError: Cannot read property 'x' of undefined
at collision (collision.js:6)"
I just cannot figure out whats going on. If someone can help me I will be so grateful, I saw similar topic but I couldn't find the answer there.
function collision(){
for(var l = bullets.length - 1; l >= 0; l--){
for( var k = enemies.length - 1; k >= 0; k--){
var e = enemies[k];
var b = bullets[l];
if(b.x > e.x && b.x < e.x + e.imgWidth/4 && b.y > e.y && b.y < e.y +
e.imgHeight/4 ){
e.enemiesHealth -= 50;
b.deletion(); // basically assigns true to b.toDelete
e.deletion(); // basically assigns true to e.toDelete
}
else if (b.toDelete){
bullets.splice(l, 1);
}
else if(e.toDelete && e.enemiesHealth == 0){
enemies.splice(k, 1);
kills += 1;
}
}
}
}
deletion()function, and its relation totoDelete? If it is what I think, it looks like there could be potential unexpected behaviour with the code. \$\endgroup\$