Craig Harkness
Craig Harkness

Reputation: 3

Why is this AS3 code generating an "Error #2006: The supplied index is out of bounds"?

So what I'm trying to do is go through each element of the array "maps" which contains 4 movieclips and look and the children within each of those movieclips to see which are of type "Block". However I'm getting a #2006 error and I'm not sure why, can anyone help please?

function findBlocks() 
{
    trace("findBlocks()");

    for (i=0; maps.length; i++)
    {
        for (var j=0; maps[i].numChildren; j++)
        {
            var mc = maps[i].getChildAt(j);
            if (mc is Block)
            {
                blocks.push(mc);
            }
        }
    }
    trace("blocks array: " + blocks);
}

Upvotes: 0

Views: 872

Answers (1)

prototypical
prototypical

Reputation: 6751

Your for loop conditions are incorrect, try this :

for (var i=0; i < maps.length; i++){
    for (var j=0; j < maps[i].numChildren; j++){
        var mc = maps[i].getChildAt(j);
        if (mc is Block){
            blocks.push(mc);
        }
    }

}

You have to remember that arrays and the display list start at 0, so the index of the last element in your lists is length-1, and in the case of a display list numChildren-1

i < maps.length

and

j < maps[i].numChildren 

are what solve the problem

Upvotes: 3

Related Questions