Category:
flash
Dynamic class names are neat
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:
[ftf w=”490″ h=”130″]
var dynamicClass:String = “page” + i;
var classRef:Class = getDefinitionByName(dynamicClass) as Class;
var page:MovieClip = new classRef();
addChild(page);
[/ftf]
30 thoughts on “Dynamic class names are neat”
October 6, 2007 at 14:29
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 at 11:56
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 at 17:04
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 at 21:57
Scotland! Woot! My as3 nonsense is spreading across the globe! :) Glad I could help.
October 24, 2007 at 17:25
Thanks from Belgium too !
Very useful !!!
May 28, 2008 at 00:07
Just was I was looking for! (trying to find my as3-workflow somewhere between pure coding and library-usage) Hvala! :)
May 28, 2008 at 00:13
..for your country collection: Thanx from Germany! :)
June 2, 2008 at 02:18
Ok… for your country collection once again, here goes a “thank you from Portugal” :P …
June 16, 2008 at 16:36
Awesome, cheers for this technique! :D
October 23, 2008 at 16:42
Genius! Thank you from Italy (one more for your collection) ;-)
November 15, 2008 at 17:56
Life’s easier, thanks pal.
November 15, 2008 at 17:57
Following this blog’s tradition, thanks from Mexico
December 5, 2008 at 00:17
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 at 18:58
Thanks Man, I have been struggling for this solution over few days.
April 17, 2009 at 14:05
Again.. thx from canada ^^ !
April 19, 2010 at 12:48
Thnx from Croatia … ;)
May 20, 2010 at 21:29
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 at 21:43
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 at 16:51
Exactly what I was looking for! Thank you a lot. :)
Greetings from Latvia!
July 29, 2010 at 11:49
Even though I don’t get it 100% how it works – it still works!
Thanks from Estonia!
September 14, 2010 at 06:42
Absolutely awesome, thank you so much!
September 22, 2010 at 07:54
…and representing the southern hemisphere: thanks from Australia! :D
October 21, 2010 at 15:16
Thanks for this. It was really usefull. Thanks from Poland :)
November 15, 2010 at 16:04
ERROR?!… if i = 1 then it returns “Variable page2 is not defined.”
November 18, 2010 at 19:34
Thanks from Atlanta, GA, USA!
December 3, 2010 at 22:47
Very info rich blog you have, hope you keep up the good standard :)
September 14, 2011 at 13:20
Thanks a lot!! =)
December 2, 2011 at 11:55
Thanks for the snippet!
February 12, 2012 at 14:04
Thank you from Bulgaria too :))
November 6, 2012 at 18:01
Thanks for your help!
cheers