So im doingI'm creating a game where waveswaves of enemies are coming to me and iI have to kill them. So
So far im, I'm doing great: it took me hours to come up with this nested loop to check for collision detection and when iI ran it the game was running great ... but for a few kills only . Sometimes iI manage to play it even for a few minutes until it crashed crashes, sometimes iI shoot twice and it crashes , and iI always get this error : "Uncaught TypeError: Cannot read property 'x' of undefined at collision (collision.js:6)" I
"Uncaught TypeError: Cannot read property 'x' of undefined
at collision (collision.js:6)"
I just cannot figure out whats going on ... ifIf someone can help me iI will be so greatful grateful, iI saw similar topic but i couldntI couldn't find the answer there. Have a nice day everyone !
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;
}
}
}
}