samedi 11 mars 2017

Looping through nested Arrays AS3

In my set of nested loops, I am scanning all the rows in my Tetris game and checking if any blocks are in that row. If a counter gets up to 10, then I know the row is full, and that row gets added to an Array that will be used in a later function to remove all those blocks. There may be a more elegant design, but this is what I have. It was working... then I changed stuff elsewhere, and now it doesn't. Headaches ensued.

So to be clear, my first for loop runs through the set of rows on the board; the inner loop runs through ALL the blocks on the board, checking if they have a row value equal to the decrementing variable. Not efficient, maybe, but it runs fast enough, so I haven't bothered to optimize yet.

for (var i:int = rowArray.length - 1; i >= 0; i--){
    for (var j:int = blockArray.length - 1; j >= 0; j--){
        if (blockArray[j].row == i){

I'm away from my desk, so this isn't the exact code but it is the same setup. I can post the exact code tomorrow if needed.

So the problem is this: Sometimes all the complete rows get eliminated correctly, but usually 2 or 3 blocks do not get added to the eliminateArray and thus do not get removed. I've followed the bug far enough to know this. When I trace out the length of the eliminateArray, it only is 37 or 38 instead of 40 (when 4 lines are cleared at once). And I have a text field on each block that shows what its row value is... everything looks fine there. So I'm believing that the issue is how I'm iterating through these loops, and in some strange scenario, a couple of blocks are being skipped, but there seems to be no rhyme or reason or pattern to it.

Does my nested for loop look "correct"?

Aucun commentaire:

Enregistrer un commentaire