Search code examples
javascriptfor-loopcollision-detection

Should this For-Loop, in theory, work?


I have a question over at collision-detection about a similar issue, but it's not exactly the same. I had an issue with a new game project (I'm trying to learn more about HTML5 Canvases and Socket.io) in which my collisions weren't working. I thought that my issue was centered on collisions, but now I'm starting to think something different. The reason I have a different issue posted here at the for-loop area is because I'm not sure if my issue is for-loop related or collision-detection related. Either way, I'd be happy to take one of my questions down.

This code is looping every frame to get the active positions of bullets and ships. If the bullet touches the ship, it'll be removed and some health points will be removed from the ship.

Tutorial I was using: http://jlongster.com/Making-Sprite-based-Games-with-Canvas

That aside, here's my checkCollisions code. It seems that the collision function is working, because when I started to log all the positions every time we had an iteration, it seemed that the position of my object was changing every single time. Is this one of those for-loop issues where I'm going to need a callback?

Thank you so much in advance for all your help. I'll be sure to upvote/select every response that helps out! :)

SOLVED! Turns out one of my arrays wasn't being passed in correctly. I'd like to thank you guys for telling me to always split it into multiple functions, that really helped me figure that one out!

// Let's start out here: I have a players[] array
//that's essentially a list of all players on the server
// and their positions. I omitted server connection functionality since that's not my error
// source.
function checkCollisions() {
    for (var i = 0; i < players.length; i++) { // Iterating through all players
        var pos = [players[i].posX, players[i].posY];
        var size = [SHIP_WIDTH, SHIP_HEIGHT]; // This is the size of each player, it's a ship game. So these are constants.
        if (players[i].userId != PLAYER.userId) { // Each player has a userId object, this is just doublechecking if we're not uselessly iterating
            for (var j = 0; j < bullets.length; j++) { // We're now looping through bullets, an array of all the bullets being shot by players
                var pos2 = bullets[j].pos;
                var size2 = BULLET_SIZE;
                var sender = bullets[j].sender;
                if (boxCollides(pos, size, pos2, size2)) { // Collision code
                    if (sender != players[i].userId) {
                        bullets.splice(j, 1);
                        i--; // Tried here with j--, and by removing the entire line. Unfortunately it doesn't work :(
                        break;
                    }
                }
            }
        }
    }
}

Solution

  • Have you tried using console.log to see where the program is breaking? This might help you determine if there are multiple bugs or if it's just this one. If there's something wrong in a previous statement, you may not know it if you've fixed the i-- / j-- ...?

    Edit: AH I see that you've fixed things, after I'd posted this. Well congrats and good job!