A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Dynamic instance/Variable names in loops

  1. #1
    Member
    Join Date
    Aug 2005
    Posts
    30

    Dynamic instance/Variable names in loops

    This has been bugging me for a while but I'll have a series of movieclips, variables, input boxes, or whatever with a name then a number as their names. For example input0, input1, input2, etc. In AS2 I could throw them into a loop like so:

    Code:
    for(i = 0; i<3; i++)
    {
         this["input"+i].text = array[i];
    }
    ...or something to that regard. How do I do this in AS3 where I can set up a loop and dynamically call the numbered instance name?

  2. #2
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    It's not really much different.
    "this" in AS3, in the document class or in a frame script, refers to the timeline; so safer to refer to the stage if these things are already placed on stage.
    You're probably having a problem with what "this" you're talking about. Try using root["input"+i] or root.stage.getChildByName("input"+i) if you're looking for fields placed directly on the stage before compiling.

  3. #3
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Put your textfields into their own array...that way you store a reference to the object instead of a name that you have to lookup later.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center