AS3 reference movieclip by .name property

FoxLift picture FoxLift · Jul 26, 2012 · Viewed 18.8k times · Source

Yay, another simple noobie as3 question.

How can I reference a movieclip by its ".name" ?

I tried searching for a solution, but I couldn't find anything. Basically I have a set of movieclips added to the stage using a loop, so the way I found to diferentiate them was by giving them a .name of "something" + the Loop's "i". So now they are named something like "something1", "something2", "something3", and so on.

Now, I need to send some to a specific frame. Usually I would do something like:

something1.gotoAndStop(2);

But "something1" isnt the instance name, just the ".name". I cant find a way to reference it.

Answer

Boyd picture Boyd · Jul 26, 2012

you want to use getChildByName("name") more info

import flash.display.MovieClip;

// create boxes
for(var i:int = 0 ; i < 4; i++){

    var box:MovieClip = new myBox(); // myBox is a symbol in the library (export for actionscript is checked and class name is myBox

    box.name = "box_" + i;
    box.x = i * 100;
    this.addChild(box);

}

// call one of the boxes

var targetBox:MovieClip = this.getChildByName("box_2") as MovieClip;
targetBox.gotoAndStop(2);