views:

37

answers:

2

I have many MovieClips and each has a name like "mcDummyClosed" and then and instance name "slideDummyClosed". Another MovieClip has a link to e.g. slideDummyClosed which I then call DummyClosed. I add a MouseEvent.CLICK event to DummyClosed.

Now without adding slideDummyClosed to the stage nor any other MovieClip can I with a string containing it's name get that instance?

I've tried using getChildByName() but that only seems to work if I've already added the MC to be found and added before. My code looks something like this:

public function lookHere(e:MouseEvent){
    //this is the function called by e.g. DummyClosed
    currentView.removeChildAt(0); //remove the MC that was here before
    var slideName:String = 'slide' + e.target.name; //the name of the instance

    currentView.addChild(??); //how do I add slideName (e.g. slideDummyClosed) here?
}
A: 

I take it you want to find a specific movieclip with just using the name of it taken from the "e.target.name" ?

I take it that you dont click the same target as you want to modify. One solution is to save all mc's in an array and then loop through it comparing their names and then returning the matching one.

lollertits
That would work, I was just hoping AS3 had a method or class that would allow me to find and call an instance of an object using it's name.
Bjorninn
the getChildbyName("") is such a function but as you noticed it only works for displayObjects which have been added to the displayList in some way. Its usually a good idea to save references of all created object in arrays/vectors så the easily can be disposed of when not used anymore.
lollertits
+1  A: 

getDefinitionByName() might work here. It looks about like what you want, but I've never used it personally.

Ullallulloo
yes from the sounds of it this is what you want. You will need to set the linkage for each clip in the library (click on properties for each MovieClip in the library and check export for ActionScript). The classname specified will be the required string. Then to use getDefinitionByName should look something like var myClass:Class = getDefinitionByName("className") as Class; var img:MovieClip = new myClass();
Allan
Doesn't getDefinitionByName just return the class but not the specific instance of that class? I've aldready created the instances of the classes before I go into lookHere.
Bjorninn