Friday, June 23, 2017

javascript - How can I clear explosions in my function?


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.



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...