Re: [Flashcoders] Re: AS2 vs. AS3: instantiate library symbols with a loop & string concatenation
Thanks to everyone who chimed in with the answer! Seems like there is a little disagreement between getDefinitionByName and getClassByName. >From some basic searches it looks like getClassByName has been replaced by getDefinitionByName, and I could only find flash.utils.getDefinitionByNamein the Adobe LiveDocs. Either way, thanks for those who helped - I didn't quite know where to loock in my trusty moock. -jonathan On Tue, Mar 4, 2008 at 4:31 PM, Cory Petosky <[EMAIL PROTECTED]> wrote: > import flash.utils.getDefinitionByName; > > for (var i:uint = 0; i < 30; ++i) { > var sym:MovieClip = new (getDefinitionByName("sym" + i) as Class)(); > > // rest > } > > On 3/4/08, jonathan howe <[EMAIL PROTECTED]> wrote: > > Sorry - somehow triggered the send mail hotkey before I was finished: > > > > On Tue, Mar 4, 2008 at 3:25 PM, jonathan howe <[EMAIL PROTECTED]> > > wrote: > > > > > > > > > > > > > Imagine I have in my library a series of MovieClips with linkage > > > identifiers like so: > > > > > > sym0 > > > sym1 > > > sym2 > > > ... > > > sym29 > > > > > > In AS2, if I wanted to create instances of each one (or perhaps > decide > > > which symbol to use based on XML data, for example), I could use a > for loop: > > > > > > for (var i:Number = 0; i < 30; i ++) { > > > var new_mc:MovieClip = parent_mc.attachMovie("sym" + i, "sym" + > i, i); > > > // do stuff with clip > > > } > > > > > > and create each symbol via passing a concatenated string as > symbolName. > > > > > > Now, in AS3, I can't think of a way to do this efficiently. In our > simple > > > example, I can only think of the following: > > > > > > for (var i:int = 0; i < 30; i ++) { > > > var sym:MovieClip; > > > switch (i) { > > > case 0: sym = new sym0(); > > > > > case 1:sym = new sym1(); > > > case 2: sym =new sym2(); > > > > > ... > > > } > > > // do stuff with clip > > > } > > > > > > > > > Is there a more efficient way to do this in AS3? You can't really > > > instantiate a class based on a stringname, can you? > > > > > > > Was I doing myself a disservice by using this technique in AS2 anyway? > > > > > > > Thanks, > > > > -jonathan > > > > ___ > > Flashcoders mailing list > > Flashcoders@chattyfig.figleaf.com > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > > > > -- > Cory Petosky : Lead Developer : PUNY > 1618 Central Ave NE Suite 130 > Minneapolis, MN 55413 > Office: 612.216.3924 > Mobile: 240.422.9652 > Fax: 612.605.9216 > http://www.punyentertainment.com > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > -- -jonathan howe :: 404.434.2321 :: 180 High St Apt 26 Portland, ME 04101 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Re: AS2 vs. AS3: instantiate library symbols with a loop & string concatenation
import flash.utils.getDefinitionByName; for (var i:uint = 0; i < 30; ++i) { var sym:MovieClip = new (getDefinitionByName("sym" + i) as Class)(); // rest } On 3/4/08, jonathan howe <[EMAIL PROTECTED]> wrote: > Sorry - somehow triggered the send mail hotkey before I was finished: > > On Tue, Mar 4, 2008 at 3:25 PM, jonathan howe <[EMAIL PROTECTED]> > wrote: > > > > > > > > Imagine I have in my library a series of MovieClips with linkage > > identifiers like so: > > > > sym0 > > sym1 > > sym2 > > ... > > sym29 > > > > In AS2, if I wanted to create instances of each one (or perhaps decide > > which symbol to use based on XML data, for example), I could use a for > loop: > > > > for (var i:Number = 0; i < 30; i ++) { > > var new_mc:MovieClip = parent_mc.attachMovie("sym" + i, "sym" + i, i); > > // do stuff with clip > > } > > > > and create each symbol via passing a concatenated string as symbolName. > > > > Now, in AS3, I can't think of a way to do this efficiently. In our simple > > example, I can only think of the following: > > > > for (var i:int = 0; i < 30; i ++) { > > var sym:MovieClip; > > switch (i) { > > case 0: sym = new sym0(); > > > case 1:sym = new sym1(); > > case 2: sym =new sym2(); > > > ... > > } > > // do stuff with clip > > } > > > > > > Is there a more efficient way to do this in AS3? You can't really > > instantiate a class based on a stringname, can you? > > > > Was I doing myself a disservice by using this technique in AS2 anyway? > > > > Thanks, > > -jonathan > > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > -- Cory Petosky : Lead Developer : PUNY 1618 Central Ave NE Suite 130 Minneapolis, MN 55413 Office: 612.216.3924 Mobile: 240.422.9652 Fax: 612.605.9216 http://www.punyentertainment.com ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Re: AS2 vs. AS3: instantiate library symbols with a loop & string concatenation
Hi Jonathon, Your AS2 method is tried and true. Here's the new and improved AS3 way of doing it. for (var i:Number = 0; i < 30; i ++) { // build the String name var symbol:String = "sym" + i; // locate the class var symbolClass:Class = getDefinitionByName(symbol) as Class; // instantiate the clip using the class var new_mc:MovieClip = new symbolClass() as MovieClip; // add clip to stage addChild(new_mc); // do stuff with clip } You may or may not need the " as MovieClip" bit on the instantiation, depending on how your class is actually defined. hth, Bob jonathan howe wrote: Sorry - somehow triggered the send mail hotkey before I was finished: On Tue, Mar 4, 2008 at 3:25 PM, jonathan howe <[EMAIL PROTECTED]> wrote: Imagine I have in my library a series of MovieClips with linkage identifiers like so: sym0 sym1 sym2 ... sym29 In AS2, if I wanted to create instances of each one (or perhaps decide which symbol to use based on XML data, for example), I could use a for loop: for (var i:Number = 0; i < 30; i ++) { var new_mc:MovieClip = parent_mc.attachMovie("sym" + i, "sym" + i, i); // do stuff with clip } and create each symbol via passing a concatenated string as symbolName. Now, in AS3, I can't think of a way to do this efficiently. In our simple example, I can only think of the following: for (var i:int = 0; i < 30; i ++) { var sym:MovieClip; switch (i) { case 0: sym = new sym0(); case 1:sym = new sym1(); case 2: sym =new sym2(); ... } // do stuff with clip } Is there a more efficient way to do this in AS3? You can't really instantiate a class based on a stringname, can you? Was I doing myself a disservice by using this technique in AS2 anyway? Thanks, -jonathan ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders -- Thanks, ~ Bob Leisle Headsprout Software & Engineering http://www.headsprout.com Where kids learn to read! ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders