寻址不是那样工作的。您不能使用单个 [] 操作来处理孩子的孩子。此外,最好使用 getChildByName 方法,因为点语法访问仅适用于发布选项“自动声明阶段实例”。
for (var u: int = 0; u < 4;u++)
{
for (var v: int = 1; v < 4;v++)
{
// Get a reference to a child and typecast it as MovieClip.
var aDigit:MovieClip = problem.getChildByName("digit" + u + "" + v) as MovieClip;
aDigit.gotoAndStop(1);
}
}
让我们解释一些事情。
首先,MovieClip 是一个动态类,这意味着您可以读取和写入其实例成员而无需实际声明它们(点和括号语法之间的区别在这里解释 Using . or [ ] to access Object properties - what's the difference? ):
var M:MovieClip = new MovieClip;
trace(M['a']); // undefined, no error raised
M['a'] = 1;
trace(M['a']); // 1
trace(M.a]); // 1
其次,MovieClip 是一个 DisplayObjectContainer,因此它可以包含子项。这些孩子有名字,您可以通过调用 getChildByName("child name going here") 方法来引用它们,并且不要忘记对结果进行类型转换,因为有各种各样的孩子,因此默认情况下它们由其基 DisplayObject 类键入.子名称不与对象成员名称相同(不过,正如我之前提到的,“自动声明阶段实例”在幕后将子引用分配给同名变量):
// We proceed working with M from above.
// Lets create a child for M.
var C:MovieClip = new MovieClip;
// MovieClip C has a name "D" as of now.
C.name = "D";
// C named "D" becomes a child of M.
M.addChild(C);
trace(M.C); // undefined, because C is not a member of M object, it's just a local variable
trace(M.D); // undefined, because D is the name of MovieClip which is child of M MovieClip but not a member of M objest.
trace(M.getChildByName("C")); // null, because M has no children with name "C".
trace(M.getChildByName("D")); // [object MovieClip] because there is indeed a child with the name "D" inside of M.
// We create a field with the name "E" inside M object.
M.E = C;
trace(M.getChildByName("E")); // null, because M has no children with name "E".
trace(M.E); // [object MovieClip] because M now contains a field E with the reference to MovieClip C named "D".