2

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

londonbird
  • 45
  • 1
  • 2
  • 7

2 Answers2

4

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.

Mims H. Wright
  • 3,049
  • 1
  • 25
  • 30
  • Yes Mims...thanks for the clarification. I'm new at all this btw...and thanks for sharing your knowledge!! I'm also learning Java but I prefer actionscript 3.0 lol. – londonbird Apr 12 '11 at 02:39
1

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

George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • Flash CS5 likes your code! After I fixed the problem (thought I fixed the problem with the code above), I got errors and it didn't like the if statement at all and had to remove it, but yours is persistently working to solve this Index Out of Bounds error. Cheers x Now I've got to clear this 1009 error lol – londonbird Apr 08 '11 at 11:32
  • strange...the ? is a just a compact if/else. Goodluck with the 1009(hunch: check for null instances) ! Also, you might find this site handy: http://www.actionscripterrors.com/ ( http://www.actionscripterrors.com/?s=1009&searchsubmit=Find ) – George Profenza Apr 08 '11 at 11:35
  • @londonbird addChildAt(uiBar, numChildren > 0 ? numChildren-1 : 0); adds uiBar at the top of the display list, which is the same as addChild(uiBar). Why is addChildAt() needed in this case ? – George Profenza Apr 08 '11 at 21:16
  • Hi George, I think you misunderstood me. I was saying that the code you gave me on 8 April worked! Cheers. – londonbird Apr 12 '11 at 02:36