Attaching movieclips to the stage in AS3 is easy. But what if we need to attach a movieclip with dynamically created name?
var page:MovieClip = new ["page"+i]; // won't work :)
But this will:
var dynamicClass:String = "page" + i; var classRef:Class = getDefinitionByName(dynamicClass) as Class; var page:MovieClip = new classRef(); addChild(page);









October 6, 2007 @ 14:29
3stripe says:
Interesting…
I usually do it more like this… not sure if it’s the same thing though?
var page:MovieClip = new MovieClip();
page.name = “page”+i;
addChild(page);
October 7, 2007 @ 11:56
seven says:
No no… let’s say you have symbols exported for ActionScript in your Flash CS3 library with the names btn1, btn2, … btnx. And you want to add them to the stage. Then you have to get them out of the library somehow.
This isn’t nor recommended nor book example, I just find this really useful in the case of emergency, cluttered library or my laziness. In many cases I often get design and animations (draft FLA) from designers, and it’s too late for movieclip/symbol reconstruction so I have to work with what I got.
Ok, buttons are lame example. I recently had to add to stage stuff whose linkage id(name) was based on location on the screen. Eg ‘pageTopLeft1′, or ‘pageTopRight8′. I know I could do it completely differently, but sometimes, strict deadlines aren’t helping my AS3 enthusiasm. :)
October 18, 2007 @ 17:04
Og2t says:
Thanks for that, I was looking exactly for it the other day — I had different modules written as AS3 classes and needed to dynamically create instances of them. Eventually, I ended up doing nasty “if” comparsion but your way is much better! Thanks! Greetings from Scotland! :)
October 18, 2007 @ 21:57
seven says:
Scotland! Woot! My as3 nonsense is spreading across the globe! :) Glad I could help.
October 24, 2007 @ 17:25
kam says:
Thanks from Belgium too !
Very useful !!!
May 28, 2008 @ 00:07
Vatrobot says:
Just was I was looking for! (trying to find my as3-workflow somewhere between pure coding and library-usage) Hvala! :)
May 28, 2008 @ 00:13
Vatrobot says:
..for your country collection: Thanx from Germany! :)
June 2, 2008 @ 02:18
xlogic says:
Ok… for your country collection once again, here goes a “thank you from Portugal” :P …
June 16, 2008 @ 16:36
Tim says:
Awesome, cheers for this technique! :D
October 23, 2008 @ 16:42
Goblin says:
Genius! Thank you from Italy (one more for your collection) ;-)
November 15, 2008 @ 17:56
walter says:
Life’s easier, thanks pal.
November 15, 2008 @ 17:57
walter says:
Following this blog’s tradition, thanks from Mexico
December 5, 2008 @ 00:17
Collin says:
Hey thanks for the tip, it works great on Library items, but was wondering how to use it on pure actionscript Classes (like in flex where I don’t have a swf to reference).
All the classes extend Sprite so i’m using that as the base type but get an error
Error #1065: Variable InlineEdit is not defined. I’m afraid I have to create an instance of every class I’d like to use before getDefinitionByName will work.December 18, 2008 @ 18:58
Mars says:
Thanks Man, I have been struggling for this solution over few days.
April 17, 2009 @ 14:05
Polo says:
Again.. thx from canada ^^ !
April 19, 2010 @ 12:48
Alen says:
Thnx from Croatia … ;)
May 20, 2010 @ 21:29
Istvan says:
I think I looked into this some time ago and didn’t find anything on it. So, I was going to go with the “IF …” statements but gave it another try … and found your post. Cool! :D Thanks :)
July 3, 2010 @ 21:43
Tim says:
Dude. I have been looking for this information for 9 months since I started teaching myself AS3 and I finally typed the right search query that brought me to you. THANK YOU THANK YOU THANK YOU!!!!!
July 4, 2010 @ 16:51
Rihards says:
Exactly what I was looking for! Thank you a lot. :)
Greetings from Latvia!
July 29, 2010 @ 11:49
Martin says:
Even though I don’t get it 100% how it works – it still works!
Thanks from Estonia!
September 14, 2010 @ 06:42
Efe says:
Absolutely awesome, thank you so much!
September 22, 2010 @ 07:54
Matt says:
…and representing the southern hemisphere: thanks from Australia! :D
October 21, 2010 @ 15:16
sun says:
Thanks for this. It was really usefull. Thanks from Poland :)
November 15, 2010 @ 16:04
Nayhan says:
ERROR?!… if i = 1 then it returns “Variable page2 is not defined.”
November 18, 2010 @ 19:34
Dave Berzack says:
Thanks from Atlanta, GA, USA!
December 3, 2010 @ 22:47
work articles says:
Very info rich blog you have, hope you keep up the good standard :)
September 14, 2011 @ 13:20
Eric says:
Thanks a lot!! =)
December 2, 2011 @ 11:55
Matthias says:
Thanks for the snippet!