#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    660
    Rep Power
    32

    How do I refer to an object on stage??


    Example:

    I have two dynamic text fields on stage named text1_field, text2_field

    I am trying to use a loop to fill those dynamic text fields

    for(var i:int=0;i<2,i++){
    this.Object("text"+[i+1]+"_field").text = hello;
    }

    Generally, how do you target objects, movieclips, etc. on stage by their instance name by using a loop??
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    7
    I'd advice you to rename your instances to textField0 and textField1, to make it easier to reference them. Then you can use an array to hold your fields, which you need for the loop.

    Code:
    var textFields:Array = new Array(2);
    textFields[0] = textField0;
    textFields[1] = textField1;
    
    for(i = 0; i < textFields.length; ++i)
    {
       textFields[i].text = "Hello " +(i+1);
    }
    Hope it helps.
    Attached Files
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    660
    Rep Power
    32
    Thanks stillwell,

    I was hoping there was a shorter process than having to create an array and just directly target an object, movieclip that is on stage.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    7
    Maybe there is, I just don't know it

    Don't really think it's a complicated process, though.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0
    Why not just use an array as icio suggested. You could then just have the one mc on stage, and then determine a cell of the array from which frame you are on???
  10. #6
  11. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3164
    So with 3 TextFields on the stage "text0, text1 and text2" you can use:
    Code:
    import flash.text.TextField;
    
    for(var i:int = 0; i < 3; i++)
       {
          (this.getChildByName("text" + i) as TextField).text = "Hello x" + i;
       }
    getChildByName lets you use the names you assign via the properties panel or when you do:

    some_clip.name = "text" + i;

    It returns an Object though so you have to cast it as I did. In the example I cast it to be a type TextField which then lets us call the ".text" property.

    getChildByName is kinda slow when it comes to access, not something you'll notice with less than a hundred things but when you get into hundreds it may become noticable. The faster method is to use getChildAt which uses the index/layer/depth of the clip instead:
    Code:
    import flash.text.TextField;
    
    for(var i:int = 0; i < 3; i++)
       {
          (this.getChildAt(i) as TextField).text = "Hello x" + i;
       }
    Same deal as before though, if you want to access any properties or functions then you have to cast it i.e. (this.getChildAt(i) as TextField).

    Comments on this post

    • stillwell agrees
    Quis custodiet ipsos custodes?

IMN logo majestic logo threadwatch logo seochat tools logo