mardi 22 novembre 2016

I need to add a value to a 2D array I created

So I created a 2D array with this code:

var grid:Array = [
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]
]

And I have a button that (in theory) will splice a value to each row, therefore expanding the grid width-wise. The issue, however, seems not to be in my code for expanding it wider, but rather when I combine the vertical increase AND the horizontal.

Here's the current code I'm attempting to use for height increase:

var insertTo:int = 1;

var temp:Array = grid[0];

grid.splice(1, 0, temp);

And here's the current code I'm attempting to use for width increase:

for (var i:int = 0; i < grid.length; i++){

    var insertTo:int = 1;

    grid[i].splice(insertTo, 0, 1); 

}

And the current unintended result after clicking the height button, then the width button (I have traces all over):

After height increase:
1,1,1,1
1,1,1,1
1,1,1,1
1,1,1,1
1,1,1,1

After width increase:
1,1,1,1,1,1
1,1,1,1,1,1
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1

And here's the intended result after doing the same in reverse:

After width increase:
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1

After height increase:
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1

Why does it work one way and not another, and how do I fix it?

Aucun commentaire:

Enregistrer un commentaire