1

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()
2
  • Please edit the question and use a code block instead of a quote block. The quote block you have used makes it difficult to read. Commented Oct 25, 2009 at 11:06
  • Just tried. Not sure if it helps? Commented Oct 25, 2009 at 11:11

3 Answers 3

1

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?

Sign up to request clarification or add additional context in comments.

3 Comments

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

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

1 Comment

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.)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.