2
\$\begingroup\$

Hi I have a function to place bombs, and a for loop that places explosions on the tiles where possible.

My problem is that I can't remove the explosions after a while. I've tried everything I can come up with so now I turn here as a last resort.

The function looks like this:

function Bomb(){
var placebomb = false;
    if(placeBomb && player.bombs != 0){
        map[player.Y][player.X].object = 2;
        var bombX = player.X;
        var bombY = player.Y;
        placeBomb = false;
        player.bombs--;
        setTimeout(explode, 3000);
    }
    function explode(){
    var explodeNorth = true;
    var explodeEast = true;
    var explodeSouth = true;
    var explodeWest = true;

    map[bombY][bombX].explosion = 1;
    delete map[bombY][bombX].object;

    for(i=0;i<=player.bombRadius;i++){  
        if(explodeNorth && map[bombY-i][bombX]){
            if(!map[bombY-i][bombX].wall){
                if(!map[bombY-i][bombX].object){
                    map[bombY-i][bombX].explosion = 1;
                }
                else                
                    var explodeNorth = false;
                    delete map[bombY-i][bombX].object;
                    map[bombY-i][bombX].explosion = 1;
            }
            else
                var explodeNorth = false;
        }

        if(explodeEast && map[bombY][bombX+i]){
            if(!map[bombY][bombX+i].wall){  
                if(!map[bombY][bombX+i].object){
                    map[bombY][bombX+i].explosion = 1;
                }
                else
                    var explodeEast = false;
                    delete map[bombY][bombX+i].object;
                    map[bombY][bombX+i].explosion = 1;
            }
            else
                var explodeEast = false;
        }

        if(explodeSouth && map[bombY+i][bombX]){    
            if(!map[bombY+i][bombX].wall){
                if(!map[bombY+i][bombX].object){
                    map[bombY+i][bombX].explosion = 1;
                }
                else
                    var explodeSouth = false;
                    delete map[bombY+i][bombX].object;
                    map[bombY+i][bombX].explosion = 1;
            }
            else
                var explodeSouth = false;
        }

        if(explodeWest && map[bombY][bombX-i]){
            if(!map[bombY][bombX-i].wall){
                if(!map[bombY][bombX-i].object){
                    map[bombY][bombX-i].explosion = 1;
                }
                else
                    var explodeWest = false;
                    delete map[bombY][bombX-i].object;
                    map[bombY][bombX-i].explosion = 1;

            }
            else
                var explodeWest = false;
        }

    }
    player.bombs++;
    }
}

If anyone can think of a good way to remove the explosion after a delay please help.

\$\endgroup\$
0

1 Answer 1

3
\$\begingroup\$

Have you tried creating an Explosion object with its own lifetime? If you add the explosion tiles that you place to an array that's managed by some sort of supervisor, then you could add a setTimeout(manager.deleteMe(X,Y), 100) in an Explosion object.

\$\endgroup\$
2
  • \$\begingroup\$ Yeah this seems to be my best alternative, was hoping I just placed my "delete" in the wrong place etc, but i think maybe an array with X Y and time of the explosion is my best bet, how would I structure it if i want to access it like you wrote (with (x,y))? \$\endgroup\$ Mar 23, 2012 at 21:11
  • \$\begingroup\$ Well, you've got delete map[bombY][bombX].object; So just make manager's deleteMe function remove the explosion from map[x][y], and replace it with whatever you use to represent unoccupied space. \$\endgroup\$
    – user14497
    Mar 23, 2012 at 21:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .