Today I almost teared myself apart untill I finally found a good solution for creating diffrent instances of a movieclip from the library! or at least I think this is a good method! The best solution I could come up with anyway and I thought to share it with you as I'm sure a lot of people are looking for a good way to dynamically create different instances of a movieclip being exported for ActionScript in the library.
Here is how the FLA is set up. we have a movieclip in the library, and it's checked for export to ActionScript. (Export in Frame 1 is also checked so the movieclip will be initialized automatically at startup) You are most probably aware that if you are using a high speed internet connection such as broadband you are able to load whole clips in one go. This is really useful when you want to watch a movieclip.
I presume that you already know the fact that if a movieclip has been checked for export to AS from the library, Flash will create a class for it blindly, OR you can setup a class for it manually and just locate the path... if not, maybe you would need to read my other post here, read that first and then get back to here.
Alright, imagine that we have set up our movieclip in the library and the path to its class is something like:
com.myFlashlab.classes.tools.hMenu.BoxStyle1
ok, now whenever you want to create multipie copies from that movieclip (Or any other class, you just need to have the full path to the class and it will work) you need to use the "getDefinitionByName" method like this:
var _myStyleClassName:String = com.myFlashlab.classes.tools.hMenu.BoxStyle1; var theClassName:Class = getDefinitionByName(_myStyleClassName) as Class; var theClass:MovieClip = new theClassName(); theClass.name = "box" + i; // i is maybe a variable being used in a loop. theClass.x = i*50; addChild(theClass)
I hope this little post will save you a lot of time and you don't have to look for duplicateMovieClip and attachMovieClip equivelents any more
Regards,
Hadi

My Flash Lab
April 5th, 2009 1:00 pm
I love you for this
April 14th, 2009 10:43 pm
what i’ve been tearing myself apart trying to figure out is, if i have a bunch of assets that are placeholders for things that would normally be loaded dynamically (i.e., a bunch of client logo PNGs that must be exported with actionscript because the .swf is required to be portable as a standalone file, yet flexible enough to quickly add and re-arrange the logos) how can i specify the asset from the library using a string instead of a hard-coded class name.
i can certainly use a uniform naming convention for a set of classes (logo_1, logo_2, logo_3, etc) and I’m now attempting to use an eval() implementation such as (pseudo):
logoEmcees[ i ] = eval( “new logo_” + i + “1)” );
if anyone has success with that or knows a better way before i hurt myself trying, please holler back.
nck
April 15th, 2009 9:10 am
@nck
Please note that I mean completly two different things when I talk about “Creating MovieClips Dynamically” vs. “Generating different instances of a movieclip from the library”
the above is actually trying to create new custome classes by a custome name.
From your post, what I think you are looking for is to actually drop different instances of a movieclip from the library, which you will understand how to do it if you take a look at this post:
http://www.emstris.com/2009/02/as3-how-to-control-a-movieclip-from-a-class/
I hope it helps.
February 4th, 2010 6:15 am
thank you, thank you, thank you.
I only wish I’d found this about five hours ago!!