XCoret Posted March 25, 2017 Share Posted March 25, 2017 First of all, sorry for my english Hello! I'm trying to make a Arkanoid game on android Phaser. When the ball hits a brick, the brick disappears and the ball bounces well. Now I have two types of bricks, a normal brick, brick and TNT brick. I've implemented that when the ball touches a TNT brick, kill adjacent bricks too, but when one of those adjacent bricks is a TNT brick, it must call the same function recursively, but something does not work ... any idea? here is my function: Quote explodeTnt: function(_tnt) { /*_tnt is a tnt brick from a group, every brick has a name that give me his type and coords example: brick.name ='brick.5_5' is a normal brick and it is in the coords x=5,y=5 : brick.name = 'tnt.4_8' is a tnt beick and it is in the coords x=4,y=8 */ var x = parseInt((_tnt.name.split('.')[1]).split('_')[0]); var y = parseInt((_tnt.name.split('.')[1]).split('_')[1]); /* adj = [ [-1, -1],[0, -1],[1, -1], [-1, 0] ,[1, 0], [-1, 1],[0, 1],[1, 1] ]; */ adj.forEach(function(ad) { var _x = x + parseInt(ad[0]); var _y = y + parseInt(ad[1]); //console.log(_x+"_"+_y); var brickname = "brick." + _x + "_" + _y; var tntname = "tnt." + _x + "_" + _y; /*bricks the brick group, all bricks are in this group*/ bricks.forEach(function(adjBrick) { if (adjBrick.name == brickname) { adjBrick.kill(); } else if (adjBrick.name == tntname) { this.explodeTnt(adjBrick); adjBrick.kill(); } }); }); } Link to comment Share on other sites More sharing options...
mattstyles Posted March 25, 2017 Share Posted March 25, 2017 In what way does not work? Does the browser hang in an infinite loop? I'm assuming that everything is fine until you get up to your conditional which decides whether to call the function with the adjacent tnt brick, it looks like you're going to get stuck in a loop there and it'll never get to the brick.kill() function. If you have two TNT bricks next to each, say, brick A and brick B, the ball hits A so you call explode(A), the function executes and grabs all of its neighbours and starts killing them, until it gets to brick B, whereby it calls explode(B), now, immediately, explode(B) executes which does the same thing, but, brick A is still alive at this point so it calls explode(A), which goes again, finds brick B (which is still alive) and calls explode(B) and the cycle repeats indefinitely. Try killing the brick before calling explode(adjBrick) again. There are quite a few different ways to achieve this though and iterating over your entire search space all the time is not efficient, but thats a separate issue. Link to comment Share on other sites More sharing options...
Recommended Posts