I have problems defining indexed array with actionscript.
The task is following. I have a board of point objects. I need to store them into one array, so I can access each point using simply it x,y coordinates. for example to get point one I want to be able use points[1][1], etc. I read the doc here http://livedocs.adobe.com/flex/3/html/help.html?content=10_Lists_of_data_2.html, and realized that I don't understand how to initialize array for my needs. (Especially when it can contain from 10 to 15 rows and columns, so it will be quite hard to use following notation: masterTaskList[0] = ["wash dishes", "take out trash"];, as suggested in docs.)
What I am doing is:
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;
}
}
But it gives me an error:
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()
The others are right -- you need to initialize your arrays as Arrays.
I'd also add that since you know the boardSize in advance of populating these arrays, you should use that value as well, to avoid the unnecessary overhead of using 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;
}
}
Then, to read the values in the way you describe, just use a getter:
private function getStone(x:uint, y:uint):StoneSprite
{
return points[x - 1][y - 1] as StoneSprite;
}