I seem to have an array scope issue. I have a global variable;
var itemConnect:Array = new Array();
Which is initialized at the start. I then have a function to populate it as a 2-d array:
// Draw connections
function initConnections() {
for (var i:Number = 0; i < anotherArray.length; i++) {
for (var j:Number = 0; j < anotherArray[i].length; j++) {
itemConnect[i] = new Array();
itemConnect[i][j] = new Shape();
}
}
}
The data structure looks something like:
CREATE: i = 0, j = 1, val = [object Shape]
CREATE: i = 0, j = 14, val = [object Shape]
CREATE: i = 1, j = 2, val = [object Shape]
CREATE: i = 1, j = 3, val = [object Shape]
CREATE: i = 1, j = 4, val = [object Shape]
CREATE: i = 1, j = 5, val = [object Shape]
CREATE: i = 1, j = 6, val = [object Shape]
...
If I try to access this array in another function, I just get this:
i = 0, j = 14, val = [object Shape]
i = 1, j = 51, val = [object Shape]
TypeError: Error #1010: A term is undefined and has no properties.
at main_fla::MainTimeline/mouseDownHandler()
I tried to initialize the array at the start as a 2-d array as follows:
var itemConnect:Array = new Array();
for (var counti = 0; counti < anotherArray.length; counti++) {
itemConnect[counti] = new Array();
}
Which produces slightly better results, but still misses many of the nodes:
i = 0, j = 14, val = [object Shape]
i = 1, j = 51, val = [object Shape]
i = 3, j = 47, val = [object Shape]
i = 6, j = 42, val = [object Shape]
i = 7, j = 42, val = [object Shape]
i = 8, j = 45, val = [object Shape]
i = 9, j = 42, val = [object Shape]
...
It seems to have scope access to just one of each of the [i] nodes, so [1][2], [1][3], [1][4] are missing – only the last [j] element appears.
What is the correct way of doing this? I also don’t know the exact size of the array at the start which may be an issue.
Thanks
Isn’t your nested loop meant to look more like this?
Notice that in this version the construction of the inner array is happening outside of the loop that is meant to be iterating it.