Actionscript 3数组范围/多维数组问题

时间:2022-11-28 21:36:58

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:

我尝试在开始时将数组初始化为2-d数组,如下所示:

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.

它似乎只能访问每个[i]节点中的一个节点,因此缺少[1] [2],[1] [3],[1] [4] - 只出现最后一个[j]元素。

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

1 个解决方案

#1


0  

Isn't your nested loop meant to look more like this?

你的嵌套循环不是看起来更像这样吗?

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();
        }
    }
}

Notice that in this version the construction of the inner array is happening outside of the loop that is meant to be iterating it.

请注意,在此版本中,内部数组的构造发生在循环之外,意味着要迭代它。

#1


0  

Isn't your nested loop meant to look more like this?

你的嵌套循环不是看起来更像这样吗?

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();
        }
    }
}

Notice that in this version the construction of the inner array is happening outside of the loop that is meant to be iterating it.

请注意,在此版本中,内部数组的构造发生在循环之外,意味着要迭代它。