Reputation: 9610
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()
Upvotes: 1
Views: 623
Reputation: 10409
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;
}
Upvotes: 0
Reputation: 25949
Idd, you have to initialize stones[x] as an Array. In C++ for instance, you can initialize a two-dimensional array in one line (with constant size I think), but in AS you can't.
If you start the loop at index 0, you could also use push, but it adds nothing to the answer of Khilon (+ it's kinda off dangerous if you should ever change the starting index of the loops).
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);
}
}
Upvotes: 1
Reputation: 2513
I don't have AS compiler at hand, but I believe that
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;
}
}
might work.
Btw, is there a reason why you start the loop at index 1?
Upvotes: 1