Simplyprofound.com
Flash, Flex, AIR, ASP.NET, PHP, AJAX, whatever it takes...

How to attachMovie in Actionscript 3

April 1, 2008 06:33 by dave

I'm converting an older Flash (AS2) kiosk application to an AIR (AS3) desktop application. The challenges are many, but it's a great learning experience. I'm reminded that it's the simple stuff that seems to matter most - those little tricks you use daily to make your projects go.

One AS2 feature that I used extensively in AS2 (as did the developer that originally wrote this particular project), was the attachMovie.

myTarget_mc.attachMovie("myLibraryMovieClip","newClip_mc",_root.getNextHighestDepth()); 

In AS3, this is dead and gone, and has been replaced with:

var newClip:MovieClip = new MyLibraryMovieClip();
myTarget_mc.addChild(newClip);

I have old habits, but I'm growing to like this.

BUT I still need to accomplish what was done in the original where I didn't KNOW the name of the object in the library. I knew if was a MovieClip, but the name was being passed into the function as a variable. How in the world do I create a new MovieClip based on a string variable?! 

Here is what I have discovered. You can retrieve the class that is created with linkage in the Flash library by name using getDefinitionByName(). Once you have that, you can create a new instance of that and add it to the stage or parent object. It looks like this:

import flash.utils.getDefinitionByName; 

var myLibraryMovieClip:Object = getDefinitionByName(variableString);
var newClip:MovieClip = new myLibraryMovieClip();
myTarget_mc.addChild(newClip);

LiveDocs for getDefinitionByName(): http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#getDefinitionByName()

And to give credit, I was turned on to this solution by a forum post over at Kirupa.


Tags: ,
Categories: Flash
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Related posts

Add comment


(Will show your Gravatar icon)  

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]