【问题标题】:actionscript: Using multidimensional arraysactionscript:使用多维数组
【发布时间】:2009-10-25 10:58:01
【问题描述】:

我在使用 actionscript 定义索引数组时遇到问题。

任务如下。我有一个点对象板。我需要将它们存储到一个数组中,所以我可以简单地使用 x,y 坐标来访问每个点。例如,要获得第一点,我希望能够使用点 [1] [1] 等。我在这里阅读了文档http://livedocs.adobe.com/flex/3/html/help.html?content=10_Lists_of_data_2.html,并意识到我不明白如何根据需要初始化数组。 (特别是当它可以包含 10 到 15 行和列时,因此很难使用以下表示法:masterTaskList[0] = [“洗盘子”,“取出垃圾”];,如文档中所建议的那样。)

我正在做的是:

for (var x:Number = 1; x<= boardSize; x++)
{
     for (var y:Number = 1; y<= boardSize; y++)
     {
    var stone:StoneSprite = new StoneSprite();
    stone.x = this.x + x*cellWidth;
    stone.y = this.y + y*cellWidth;
    stones[x][y] = stone;
     }
} 

但它给了我一个错误:

RangeError: Index '1' specified is out of bounds.   at mx.collections::ListCollectionView/getItemAt()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:422]    at mx.collections::ListCollectionView/http://www.adobe.com/2006/actionscript/flash/proxy::getProperty()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:698]  at components::Board/placeStonesInNodes()[/Users/oleg/jin/goclub/trunk/goapp/usersList/src/components/Board.as:60]  at components::Board/creationComplete()[/Users/oleg/jin/goclub/trunk/goapp/usersList/src/components/Board.as:44]    at flash.events::EventDispatcher/dispatchEventFunction()    at flash.events::EventDispatcher/dispatchEvent()

【问题讨论】:

  • 请编辑问题并使用代码块而不是引号块。您使用的引用块使其难以阅读。
  • 刚试过。不确定它是否有帮助?

标签: apache-flex actionscript-3 arrays


【解决方案1】:

我手头没有AS编译器,但我相信

for (var x:Number = 1; x<= boardSize; x++)
{
     stones[x] = new Array();
     for (var y:Number = 1; y<= boardSize; y++)
     {
        var stone:StoneSprite = new StoneSprite();
        stone.x = this.x + x*cellWidth;
        stone.y = this.y + y*cellWidth;
        stones[x][y] = stone;
     }
}

可能会起作用。

顺便说一句,你有理由从索引 1 开始循环吗?

【讨论】:

  • 是的,根据游戏情况,符号应该从1开始。现在试试你的代码
  • 嗯...奇怪的是:语法错误:在左括号前需要分号。就行 stone[x] = new Array();
  • 确实很奇怪。石头[x] = 新数组();对我来说编译得很好。
【解决方案2】:

Idd,你必须将stones[x] 初始化为一个数组。例如,在 C++ 中,您可以在一行中初始化一个二维数组(我认为具有恒定大小),但在 AS 中您不能。

如果你从索引 0 开始循环,你也可以使用 push,但它对 Khilon 的答案没有任何帮助(+ 如果你应该更改循环的起始索引,这有点危险)。

for (var x:Number = 0; x< boardSize; x++)
{
     stones.push(new Array());
     for (var y:Number = 0; y< boardSize; y++)
     {
        var stone:StoneSprite = new StoneSprite();
        stone.x = this.x + x*cellWidth;
        stone.y = this.y + y*cellWidth;
        stones[x].push(stone);
     }
}

【讨论】:

  • 抱歉编辑——我误点击了向下箭头,意思是点击向上。 :)(所以迫使我进行编辑以纠正错误。)
【解决方案3】:

其他人是对的——你需要将你的数组初始化为数组。

我还要补充一点,由于您在填充这些数组之前就知道 boardSize,因此您也应该使用该值,以避免使用 Array.push 产生不必要的开销:

var points:Array = new Array(boardSize);

for (var i:uint = 0; i < points.length; i++)
{
    points[i] = new Array(boardSize);

    for (var j:uint = 0; j < boardSize; j++)
    {
        var s:StoneSprite = new StoneSprite();
        // Do your work on s...

        points[i][j] = s;
    }
}

然后,要以您描述的方式读取值,只需使用 getter:

private function getStone(x:uint, y:uint):StoneSprite
{
    return points[x - 1][y - 1] as StoneSprite;
}

【讨论】:

    猜你喜欢
    • 2016-03-03
    • 2013-06-07
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    相关资源
    最近更新 更多