I need to add a value to the 2D array that 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 (theoretically) will combine the value for each row, so it expands the width of the grid. The problem, however, is not in my code to expand it wider, but rather when I combine vertical magnification and horizontal.

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

var insertTo:int = 1;

var temp:Array = grid[0];

grid.splice(1, 0, temp);

And here is the current code I'm trying to use to increase the width:

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):

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 is the intended result by doing the same in reverse order:

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 in one direction and not in another, and how to fix it?

+4
1

Array . , concat() slice() . , , , , . , . , .

var insertTo:int = 1;

var temp:Array = grid[0].concat(); // clone

grid.splice(1, 0, temp);
+2

Source: https://habr.com/ru/post/1661610/


All Articles