10 Replies Latest reply on Sep 30, 2011 6:44 AM by Ron Colmen

    loop contents in an Array?

    Ron Colmen Level 2

      Hi...

       

      Is it possible to to loop contents in an Array? Something like this?

       

      for (var s:Number = 1; s<21; s++) {

          var me = ["tfNm"+(s)];

          var mcB = ["_root.gmb_mc."+me+".tf"];

          var mcC = ["_root.gmbb_mc."+me+".tf"];

          //trace (mcB)

       

      var mcA:Array = [mcB, mcC, _root.F5];

      /////

      }

        • 1. Re: loop contents in an Array?
          Ned Murphy Adobe Community Professional & MVP

          Can you add a comment for each line of that code to explain your intentons for each?  Otherwise I would have to say you cannot do what you are trying to do the way you are trying to do it.  My guess would be...

           

          for (var s:Number = 1; s<21; s++) {

              var me = "tfNm"+String(s);

              var mcB = _root.gmb_mc[me].tf;

              var mcC = _root.gmbb_mc[me].tf;

           

              var mcA:Array = [mcB, mcC, _root.F5];

          }

          • 2. Re: loop contents in an Array?
            Ron Colmen Level 2

            Thanks Ned.

             

            for (var s:Number = 1; s<21; s++) {

                var me = ["tfNm"+(s)]; // expecting to output tfNm1, tfNm2, tfNm3, ..... tfNm20

                var mcB = ["_root.gmb_mc."+me+".tf"];// expecting to output _root.gmb_mc.tfNm1.tf, _root.gmb_mc.tfNm2.tf, _root.gmb_mc.tfNm3.tf, .... _root.gmb_mc.tfNm20.tf

                var mcC = ["_root.gmbb_mc."+me+".tf"];// expecting to output _root.gmbb_mc.tfNm1.tf, _root.gmbb_mc.tfNm2.tf, _root.gmbb_mc.tfNm3.tf, .... _root.gmbb_mc.tfNm20.tf

                //trace (mcB)

             

            var mcA:Array = [mcB, mcC, _root.F5]; // expecting to output _root.gmb_mc.tfNm1.tf, _root.gmb_mc.tfNm2.tf, _root.gmb_mc.tfNm3.tf, .... _root.gmb_mc.tfNm20.tf,   _root.gmbb_mc.tfNm1.tf, _root.gmbb_mc.tfNm2.tf, _root.gmbb_mc.tfNm3.tf, .... _root.gmbb_mc.tfNm20.tf, _root.F5

             

            End result: I want mcA Array to contain

            var mcA:Array = [_root.gmb_mc.tfNm1.tf, _root.gmb_mc.tfNm2.tf, _root.gmb_mc.tfNm3.tf, .... _root.gmb_mc.tfNm20.tf,   _root.gmbb_mc.tfNm1.tf, _root.gmbb_mc.tfNm2.tf, _root.gmbb_mc.tfNm3.tf, .... _root.gmbb_mc.tfNm20.tf, _root.F5]

            }

            • 3. Re: loop contents in an Array?
              Ned Murphy Adobe Community Professional & MVP

              Did you try what I guessed at?

              • 4. Re: loop contents in an Array?
                Ron Colmen Level 2

                yes. It worked in my code.

                 

                But wy does this trace show undefined when I test it a new fla?

                 

                //

                for (var s:Number = 1; s<21; s++) {

                    var me = "tfNm"+String(s);

                    var mcB = _root.gmb_mc[me].tf;

                    var mcC = _root.gmbb_mc[me].tf;

                    var mcA:Array = [mcB, mcC, _root.F5];

                trace (mcA) // undefined

                }

                • 5. Re: loop contents in an Array?
                  Ned Murphy Adobe Community Professional & MVP

                  On second thought... if you want to have the final array holding all of the values, then you cannot have it in the loop the way you have it.  You will need to create two arrays for all of your mcB and mcC pieces and after the looping is all done you will want to concatenate those arrays and the _root.F5 into the mcA array.

                   

                  var mcA:Array = new Array();

                  var mcB:Array = new Array();

                  var mcC:Array = new Array();

                   

                  for (var s:Number = 1; s<21; s++) {

                      var me = "tfNm"+String(s);

                      mcB.push(_root.gmb_mc[me].tf);

                      mcC.push(_root.gmbb_mc[me].tf);

                  }

                   

                  mcA = mcA.concat(mcB);

                  mcA = mcA.concat(mcC);

                  mcA.push(_root.F5);

                  • 6. Re: loop contents in an Array?
                    Ron Colmen Level 2

                    for some reason the trace in my code shows he following output

                     

                    undefined,undefined,_level0.F5

                    • 7. Re: loop contents in an Array?
                      Ron Colmen Level 2

                      oh! I missed your last posting...

                       

                      I'll give it a try now. Thanks.

                      • 8. Re: loop contents in an Array?
                        Ron Colmen Level 2

                        the trace shows

                         

                        _level0.gmb_mc.tfNm1.tf,undefined,undefined,...undefined,_level0.gmbb_mc.tfNm2.tf,undefine d,undefined,.....,undefined,_level0.F5

                        • 9. Re: loop contents in an Array?
                          Ned Murphy Adobe Community Professional & MVP

                          That gives the impression you do not have all the objects that your code is looking for.  Do you have an object... _level0.gmb_mc.tfNm2.tf, etc...?

                          • 10. Re: loop contents in an Array?
                            Ron Colmen Level 2

                            Oh ok... tfNm movieclips are created dynamically... and this code is there ready for action from tfNm1.tf to tfNm20.tf

                             

                            Thanks Ned!!! (I leaned something new here!)