错误 2006 提供的索引超出范围
不幸的是,有一天另一个问题 - 这段代码的最后一行是罪魁祸首:
uiBar = new mcUiBar();
uiBar.x=-15;
uiBar.y=-5;
addChildAt(uiBar, numChildren-1);
现在我研究了,所以我知道它与数组比任何东西都大有关,但我没有弄清楚。我很困惑。我将不胜感激你的帮助。干杯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
超出范围错误基本上是说您为索引提供的值“超出显示对象容器中索引数组的范围”。可接受的范围是从
0
到n+1
,其中 n 是最顶层子项的索引。另一种说法是0
到numChildren
。所以乔治是对的,当numChildren - 1 = -1
时你就会遇到问题。如果您尝试将子级添加到next-to-top层,请使用上面的 if 语句。但是,如果您只是尝试将其添加到顶层,则应该使用同义的
addChildAt(child, numChildren)
或addChild(child)
。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
ton+1
where n is the topmost child's index. Another way to say this is0
tonumChildren
. So George is right, you're going to have problems whennumChildren - 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)
oraddChild(child)
which are synonymous.代码太少,但最后一行:
addChildAt(uiBar, numChildren-1);
似乎是问题所在。如果还没有添加子项(numChildren 为 0)会发生什么?
这应该会引发错误,因为您尝试在深度/索引 -1 添加 uiBar
尝试
addChildAt(uiBar, numChildren > 0 ? numChildren-1 : 0);
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);