我似乎有一个数组范围问题。我有一个全局变量;
var itemConnect:Array = new 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();
}
}
}
数据结构类似于:
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]
...
如果我尝试在另一个函数中访问此数组,我只是得到这个:
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()
我尝试在开始时将数组初始化为2-d数组,如下所示:
var itemConnect:Array = new Array();
for (var counti = 0; counti < anotherArray.length; counti++) {
itemConnect[counti] = new Array();
}
产生稍好的结果,但仍然遗漏了许多节点:
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]
...
它似乎只能访问每个[i]节点中的一个,因此[1] [2],[1] [3],[1] [4]丢失 - 只有最后一个[j] ]元素出现。
这样做的正确方法是什么?我也不知道在开始时阵列的确切大小可能是一个问题。
由于
答案 0 :(得分:0)
你的嵌套循环是不是看起来更像这样?
function initConnections() {
for (var i:Number = 0; i < anotherArray.length; i++) {
itemConnect[i] = new Array();
for (var j:Number = 0; j < anotherArray[i].length; j++) {
itemConnect[i][j] = new Shape();
}
}
}
请注意,在这个版本中,内部数组的构造发生在循环之外,意味着迭代它。