londonbird
londonbird

Reputation: 45

Error 2006 The Supplied Index is Out of Bounds

Another day another problem unfortunately- the last line of this piece of code is the culprit:

                    uiBar = new mcUiBar();
        uiBar.x=-15;
        uiBar.y=-5;
        addChildAt(uiBar, numChildren-1);

Now I researched and so I know it has something to with the array being larger than whatever, but I'm not figuring it out. I'm stumped. I would appreciate your help. Cheers

Upvotes: 2

Views: 13294

Answers (2)

Mims H. Wright
Mims H. Wright

Reputation: 3037

The out of range error basically is saying that the value you're providing for the index is "out of range" of the array of indexes in the display object container. The acceptable range is from 0 to n+1 where n is the topmost child's index. Another way to say this is 0 to numChildren. So George is right, you're going to have problems when numChildren - 1 = -1.

If you're trying to add the child to the next-to-top layer, use the if statement above. However, if you're just trying to add it to the top layer, you should either use addChildAt(child, numChildren) or addChild(child) which are synonymous.

Upvotes: 4

George Profenza
George Profenza

Reputation: 51847

Too little code, but that last line: addChildAt(uiBar, numChildren-1); seems to be the problem.

What happens if there are no children added yet (numChildren is 0) ? That should throw an error because you're trying to add uiBar at depth/index -1

try addChildAt(uiBar, numChildren > 0 ? numChildren-1 : 0);

Upvotes: 1

Related Questions