Using

this["myObject" + idNumber];

-- whether myObject is a MovieClip or a textField or whatever, is from 
an AS2 technique where you would declare MCs or TXTs on the stage with a 
for loop, or actually have them declared on the Flash IDE stage at 
authortime, so it does make sense, it's just old skool. I cut my teeth 
on techniques like this, back in the day.

But you're right, putting newly declared objects in an array is a better 
method than storing the id or "index" in the instance name itself.

Of course now we have much more sophisticated iterative instantiation 
techniques like Renderers. Wish to heck we had them five years ago... :)


_______________________________________________________________

Joseph Balderson, Flash Platform Developer | http://joeflash.ca


Gordon Smith wrote:
> You can't use code like
> 
>  
> 
>     this["movie_number_" + idNumber];
> 
>  
> 
> unless you've pre-declared vars (or getter/setters) named movie_number0, 
> movieNumber1, movieNumber2, etc. -- which doesn't make any sense because 
> you usually don't know how many you'll need -- or made your class 
> dynamic -- which isn't recommended because dynamic vars are slower. 
> Otherwise, you'll get a runtime error that, for example, movie_number3 
> isn't a valid property on your class. (I don't remember the exact 
> wording of the error message.)
> 
>  
> 
> So forget about that. You need to understand how to use an Array to keep 
> track of multiple instances. It's easy:
> 
>  
> 
> 1. Declare an instance var of type Array and initialize it to an empty 
> Array:
> 
>  
> 
> public var textBoxes:Array = [];
> 
>  
> 
> 2. Every time to create a new TextBox, push it into the Array. For 
> example, inside some method, do
> 
>  
> 
> var textBox:TextBox = new TextBox();
> 
> textBox.foo = bar;
> 
> addChild(textBox);
> 
> textBoxes.push(textBox);
> 
>  
> 
> 3. In any other method, you can refer to textBox[i] to get the ith one 
> you created.
> 
>  
> 
> Gordon Smith
> 
>  
> 
> ------------------------------------------------------------------------
> 
> *From:* flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] 
> *On Behalf Of *David Pariente
> *Sent:* Thursday, June 19, 2008 7:18 AM
> *To:* flexcoders@yahoogroups.com
> *Subject:* Re: [flexcoders] Re: string to actual actionscript code?
> 
>  
> 
> Wow, that answer will really help me! :)
> 
> What if it's not movies, but some other object that i wanna create 20 times?
> I thought it should get inside some kind of Array, but not sure if its 
> this way too.
> 
> i.e.
> 
> var MyTextBox:TextBox=new textBox();
> 
> what if i need N instances of that MyTextBox? Should i create an Array 
> before that? What's the right way to put those  textBoxes into the  Array?
> 
> I'm sorry for the basic of my question....but im quite new to AS3 and 
> OOP, and i did use and misuse of evals a lot in AS1 and AS2.
> 
> Thanx a lot for the help :)
> 
> 
> ----- Mensaje original ----
> De: Josh McDonald <[EMAIL PROTECTED]>
> Para: flexcoders@yahoogroups.com
> Enviado: jueves, 19 de junio, 2008 14:16:50
> Asunto: Re: [flexcoders] Re: string to actual actionscript code?
> 
> No worries, the equivalent of
> 
> eval("movie_number_" + idNumber);
> 
> would be:
> 
> this["movie_number_" + idNumber];
> 
> To explain, in actionscript 3 (and in Javascript), these two references 
> are equivalent:
> 
> this.someField = true;
> this["someField"] = true;
> 
> trace("the value is " + anObject.button7) ;
> trace("the value is " + anObject["button7"]);
> 
> Notice the fact that it's a string. You can also use numbers (this is 
> how arrays work), and other objects, but with objects the runtime simply 
> calls .toString() and then goes ahead with the string, IIRC.
> 
> -Josh
> 
> On Thu, Jun 19, 2008 at 9:20 PM, David Pariente <xxmapachexx@ yahoo.es 
> <mailto:[EMAIL PROTECTED]>> wrote:
> 
> Thnx for the psicological help :)
> 
> Tecnically i come from AS1 and AS2 where i used to create multiple 
> copies of movieclips, and created with a name as:
> 
> eval("movie_number_"+idnumber);
> 
> maybe i should need an easy example of how to create multiple objects 
> dinamically, and most important, how to access them later.
> 
> Thnx, u guys are kind ;)
> 
> ----- Mensaje original ----
> De: Josh McDonald <[EMAIL PROTECTED] com <mailto:[EMAIL PROTECTED]>>
> Para: [EMAIL PROTECTED] ups.com <mailto:flexcoders@yahoogroups.com>
> Enviado: martes, 17 de junio, 2008 0:50:17
> Asunto: Re: [flexcoders] Re: string to actual actionscript code?
> 
> Gordon,
> 
> I can live without eval() and associated evilness, but I'd sell my left 
> testicle for the ability to mark a Proxy as extending or implementing 
> various classes or interfaces.
> 
> Mario - As for this sort of pseudo-eval that theyou're after, you could 
> definitely cook up something similar to that without *too much* work, 
> but I don't think it's the correct solution to whatever the actual root 
> problem is. We need more information as to context to be more help :)
> 
> -Josh
> 
> On Tue, Jun 17, 2008 at 4:27 AM, Gordon Smith <[EMAIL PROTECTED] com 
> <mailto:[EMAIL PROTECTED]>> wrote:
> 
>     Why are you lost without eval()? What would you use it to do? Many
>     developers think they need it when they really don't; there are
>     often other ways to accomplish what they're trying to do.
> 
>      
> 
>     Gordon Smith
> 
>     Adobe Flex SDK Team
> 
>      
> 
>     ------------------------------------------------------------------------
> 
>     *From:* [EMAIL PROTECTED] ups.com
>     <mailto:flexcoders@yahoogroups.com> [mailto:[EMAIL PROTECTED]
>     ups.com <mailto:flexcoders@yahoogroups.com>] *On Behalf Of *David
>     Pariente
> 
> 
>     *Sent:* Monday, June 16, 2008 7:35 AM
> 
>     *To:* [EMAIL PROTECTED] ups.com <mailto:flexcoders@yahoogroups.com>
> 
> 
>     *Subject:* Re: [flexcoders] Re: string to actual actionscript code?
> 
>      
> 
>     They answer u about eval() cause that was what eval() was for. I
>     used it so often in AS1 and AS2. Lost now in AS3 without it :(
> 
>     ----- Mensaje original ----
>     De: mariovandeneynde <mariovandeneynde@ yahoo.com
>     <mailto:[EMAIL PROTECTED]>>
> 
>     Para: [EMAIL PROTECTED] ups.com <mailto:flexcoders@yahoogroups.com>
> 
> 
>     Enviado: lunes, 16 de junio, 2008 12:08:26
>     Asunto: [flexcoders] Re: string to actual actionscript code?
> 
>     No, I'm just wondering if there is a way to convert a string to actual
>     actionscriptcode. ..
> 
>     --- In [EMAIL PROTECTED] ups.com
>     <mailto:flexcoders%40yahoogroups.com>, "Michael Schmalle"
>     <teoti.graphix@ ...> wrote:
>     >
>     >  Hi,
>     >
>     >  There is no eval() in actionscript if that is what you are wondering.
>     >
>     >  Mike
>     >
>     >  On Mon, Jun 16, 2008 at 5:33 AM, mariovandeneynde <
>     >  mariovandeneynde@ ...> wrote:
>     >
>     >  > Greetings,
>     >  >
>     >  > I was wondering if the following situation would be possible:
>     >  >
>     >  > imagine having a string like
>     >  >
>     >  > var query:String = "Object.property. toString( ) ==
>     somevalue.toString( )";
>     >  >
>     >  > and that the string somehow could be converted to actual
>     actionscript
>     >  > code...
>     >  >
>     >  > if(query){
>     >  > trace("your actionscriptquery worked");
>     >  > }
>     >  > else{
>     >  > trace("your actionscriptquery did not worked");
>     >  > }
>     >  >
>     >  >
>     >  >
>     >
>     >
>     >
>     >  --
>     >  Teoti Graphix, LLC
>     >  http://www.teotigra phix.com <http://www.teotigraphix.com>
>     >
>     >  Teoti Graphix Blog
>     >  http://www.blog. teotigraphix. com <http://www.blog.teotigraphix.com>
>     >
>     >  You can find more by solving the problem then by 'asking the
>     question'.
>     >
> 
>      
> 
>     ------------------------------------------------------------------------
> 
> 
>     Enviado desde Correo Yahoo!
>     
> <http://us.rd.yahoo.com/mailuk/taglines/isp/control/*http:/us.rd.yahoo.com/evt=52431/*http:/es.docs.yahoo.com/mail/overview/index.html>
>     La bandeja de entrada más inteligente.
> 
> 
> 
> 
> -- 
> "Therefore, send not to know For whom the bell tolls. It tolls for thee."
> 
> :: Josh 'G-Funk' McDonald
> :: 0437 221 380 :: [EMAIL PROTECTED] com <mailto:[EMAIL PROTECTED]>
> 
>  
> 
> ------------------------------------------------------------------------
> 
> 
> Enviado desde Correo Yahoo! 
> <http://us.rd.yahoo.com/mailuk/taglines/isp/control/*http:/us.rd.yahoo.com/evt=52431/*http:/es.docs.yahoo.com/mail/overview/index.html>
> La bandeja de entrada más inteligente.
> 
> 
> 
> 
> -- 
> "Therefore, send not to know For whom the bell tolls. It tolls for thee."
> 
> :: Josh 'G-Funk' McDonald
> :: 0437 221 380 :: [EMAIL PROTECTED] com <mailto:[EMAIL PROTECTED]>
> 
>  
> 
> ------------------------------------------------------------------------
> 
> 
> Enviado desde Correo Yahoo! 
> <http://us.rd.yahoo.com/mailuk/taglines/isp/control/*http:/us.rd.yahoo.com/evt=52431/*http:/es.docs.yahoo.com/mail/overview/index.html>
> La bandeja de entrada más inteligente.
> 
> 

Reply via email to