Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
    
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))? –  hustlerinc Mar 23 '12 at 21:11
    
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. –  K.G. Mar 23 '12 at 21:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.