good day 🙂
Why is it that when i edit the hold:Array, the array:Array also gets editted?
To give an example:
function func(2, 2) { //x, y COORDINATE
var hold = array[2]; //GET COLUMN OF ARRAY
hold[2] = 2; //SET hold[x] to 2
trace(array[2][2]) //SAME AS hold[x] *but i didn't change array[x]'s value!*
}
STEP BY STEP analysis
array[] looks like this (for example):
1,1,1,1
1,1,1,1
1,1,1,1
1,1,1,1
Thus, var hold = array[y]: (where y=2)
1,1,1,1
and hold[x] = 2 (where x=2)
1,2,1,1
Now, tracing array[y][x] (where y=2, x=2)
1,2,1,1
But array[2][2] should be 1,1,1,1, because we didn’t edit it’s value!
Question
Why does array[] get edited when i only edited hold[]
This is because arrays (typeof will give Object) are passed by reference. To copy its values you need to clone an array in ActionScript.
Here’s an explanation of this for ActionScript 2.0 (which also applies to ActionScript 3.0 but I couldn’t find the version of this article for the latter).