3 Replies Latest reply on Nov 29, 2010 4:40 AM by Ned Murphy

    How to access child movieclip info of an object in an array?

    celebritymusic Level 1

      Hi

       

      I've dynamically created a whole bunch of movieclips.

       

      I've given each clip a name based on a variable number:

       

           mc.name = "mc"+i;

       

      I've also use addChild to add a couple of dynamic text fields to each movieClip, named myText1 and myText2.

       

      I then push each movieClip object into an array:

       

           myArray.push(mc);

       

      When I addChild the movieClips, they display fine, complete with each textField.  And if I use the following loop to trace the name of each clip in the array, I get:

       

       

           for(var i=0; i<myArray.length; i++)

       

                {

                     trace(myArray[i].name);

                }

       

      output:

       

           mc1

           mc2

           mc3

           mc4

      etc

       

       

      What I want to now is be able to access the text fields within each movieclip in the array.  However, I am getting errors when I try different ways.  For example:

       

           for(var i=0; i<myArray.length; i++)

       

                {

                     trace(myArray[i].myText1.text);

                }

       

      gives the error: A term is undefined and has no properties.

       

       

      How do you access the values and contents of the children of movieClip objects that are stored in arrays?

       

      Thanks

       

      Shaun

        • 1. Re: How to access child movieclip info of an object in an array?
          Ned Murphy Adobe Community Professional & MVP

          For whatever reason, dynamically added children cannot be targeted that way.  If you added the textfields dynamically you may need to use getChildByName() to target them.  It partly depends on how you created them and whether or not you have direct access to them.  Aside from that you could also assign the textfields to variables that you create for the mc and target those by their variable names.

           

          The following demos these two approaches:

           

          var mc:MovieClip = new MovieClip();
          addChild(mc);

           

          var tf:TextField = new TextField();
          tf.text = "this is text";
          mc.addChild(tf);

           

          // first way

          tf.name = "tfield";
          trace(TextField(mc.getChildByName("tfield")).text);

           

          // second way

          mc.tfid = tf;
          trace(mc.tfid.text);

           

          You could also store the textfields in arrays as they are created and have direct access to them with out the need to target the mc... the index should be the same as that which you use for the mc anyways.

          • 2. Re: How to access child movieclip info of an object in an array?
            celebritymusic Level 1

            That's beaut Ned

             

            I used the getChild method.

             

            This also highlighted an error in my thinking in that I wasn't taking into account that each text field has an incremented name when it's created - I was trying to make it access myText instead of "myText"+i.

             

            Very much appreciated - I'm starting to get my head around the getChildByName functionality now.

             

            One thing, where you said "It partly depends on how you created them and whether or not you have direct access to them." - what exactly do you mean by that, and what determines if you have direct access?

             

            Thanks again.

             

            Shaun

            • 3. Re: How to access child movieclip info of an object in an array?
              Ned Murphy Adobe Community Professional & MVP

              If you created the object dynamically, then when you doid it was done as a var assignment, like I just showed...

               

              var mc:MovieClip = new MovieClip();
              addChild(mc);

               

              var tf:TextField = new TextField();
              tf.text = "this is text";
              mc.addChild(tf);

               

              // I don't need to target tf via mc because I have direct access to it even though it is a child...

               

              trace(tf.text);

               

              But because you may be creating numerous tf's in some loop, you could only target the last one created, which is why placing them in an array would work just as well as the other two approaches I showed... in that way you store a direct reference to each instance that was created...  tfArray[i].text would give the same desired result.

              1 person found this helpful