按名称访问事物很容易出错。如果你是新手,这不是一个好习惯。我认为更安全的方法是存储对您在循环中创建的事物的引用,例如在数组中,并通过它们的索引引用它们。
例子:
var boxes:Array = [];
const NUM_BOXES:int = 4;
const SPACING:int = 100;
// create boxes
for(var i:int = 0 ; i < NUM_BOXES:; i++){
var box:MovieClip = new MovieClip();
// You can still do this, but only as a label, don't rely on it for finding the box later!
box.name = "box_" + i;
box.x = i * SPACING;
addChild(box);
// store the box for lookup later.
boxes.push(box); // or boxes[i] = box;
}
// talk to the third box
const RESET_FRAME:int = 2;
var targetBox:MovieClip = boxes[2] as MovieClip;
targetBox.gotoAndStop(RESET_FRAME);
请注意,我还用常量和变量替换了许多松散的数字,这也将帮助您的编译器发现错误。