Skip to main content
Minor formatting, added info from the comments.
Source Link
Vaillancourt
  • 16.4k
  • 17
  • 56
  • 61

Collision detection between two arrays with nested looploops: why do I get a crash?

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;
            }
        }
    }
}

Collision detection between two arrays with nested loop

So im doing a game where waves of enemies are coming to me and i have to kill them. So far im 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 crashed , 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 greatful , i saw similar topic but i couldnt 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();
            e.deletion();
        }
        else if (b.toDelete){
            bullets.splice(l, 1);
        }
        else if(e.toDelete && e.enemiesHealth == 0){
            enemies.splice(k, 1);
            kills += 1;
        }
    }
}
}

Collision detection between two arrays with nested loops: why do I get a crash?

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;
            }
        }
    }
}
edited title
Link

The game runs fine for a few seconds and sometimes minutes and then it gives an error Collision detection between two arrays with nested loop

Source Link

The game runs fine for a few seconds and sometimes minutes and then it gives an error

So im doing a game where waves of enemies are coming to me and i have to kill them. So far im 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 crashed , 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 greatful , i saw similar topic but i couldnt 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();
            e.deletion();
        }
        else if (b.toDelete){
            bullets.splice(l, 1);
        }
        else if(e.toDelete && e.enemiesHealth == 0){
            enemies.splice(k, 1);
            kills += 1;
        }
    }
}
}