views:

147

answers:

3

Hi!

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%5FLists%5Fof%5Fdata%5F2.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()

Could you please suggest me a way to solve my problem

+1  A: 

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?

Carlos
yes, according to the game conditions, notation should start from 1.Trying your code now
Oleg Tarasenko
Hm...Strange it says:Syntax error: expecting semicolon before leftbracket. on the line stones[x] = new Array();
Oleg Tarasenko
Strange indeed. stones[x] = new Array(); compiles just fine for me.
Carlos
+1  A: 

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);
     }
}
Lieven Cardoen
Sorry for the edit -- I clicked the down arrow by mistake, meaning to click the up. :) (SO forced me to make an edit to correct the mistake.)
Christian Nunciato
A: 

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;
}
Christian Nunciato