6 Replies Latest reply: Jul 1, 2013 3:12 PM by Charine -FB buggoop RSS

    Looping through an array more that once

    Charine -FB buggoop

      I have simple array of movie clips that loop through once according to the code below:

       

      private var  lettersL1:Array = [a,a,f,f,f,g,h,i,i,n,n,n,o,o,o,o,s,s,t,t,t];

       

       

                for (var i: Number  =0; i < lettersL1.length; i++)

                 

                      {

                          trace (lettersL1[i]);                  

                      }

       

      This works, but for my game to work I need the loop to run three times.

       

      Of cause adding    i< lettersL1.length;    doesn't work. And I really would prefer not to hard code it with adding a number instead of .length.

       

      What is the correct notation?

       

      Thanks

      Charine

        • 1. Re: Looping through an array more that once
          kglad CommunityMVP

          use:

           

           

           

          private var  lettersL1:Array = [a,a,f,f,f,g,h,i,i,n,n,n,o,o,o,o,s,s,t,t,t];

           

           

                    for (var i:int  =0; i < lettersL1.length*3; i++)

           

                          {

                              trace (lettersL1[i%lettersL1.length]);                  

                          }

           


          • 2. Re: Looping through an array more that once
            Charine -FB buggoop Community Member

            Hi kglad,

             

            When I do this: for (var i: Number  =0; i < lettersL1.length*3; i++)

            my swf starts to run, but it fails to load all the objects.

             

            I get this error

            TypeError: Error #1010: A term is undefined and has no properties.

             

            All I did was add *3

             

            Is there an import flash.maths something that I must do, or why do I get this message?

             

            Thanks

            Charine

            • 3. Re: Looping through an array more that once
              dharmk Adobe Employee

              You might have missed the mod operator: ( lettersL1[ i % lettersL1.length] )

              • 4. Re: Looping through an array more that once
                Charine -FB buggoop Community Member

                Thanks dharmk,

                 

                I added that. I wonder if it should be in this classes constructor function or lower down? Heres my entire constructor function so far:

                 

                public function LetterArray()

                        {

                          for (var i: Number  =0; i < lettersL1.length*3; i++)

                          {

                               trace (lettersL1[i%lettersL1.length]);

                                                                                                       

                            lettersL1[i].x = 399;

                            lettersL1[i].y= 30;

                            addChild(lettersL1[i]);

                                }

                 

                           

                            trace("Main construtor is working");

                                                                           

                            //GameLoop

                            initText();

                            initLetters();

                            initLetterWasClickedOn();

                            initCheckIfLetterIsCorrect();

                            initInGameAnimation();

                            initRemoveLetterChildren();

                            initPointsGiven();

                           

                        }

                 

                 

                It traces the all the objects in the array once to the output window [object a,b,c,...] and then the next time (because I spesed *3) if thrace the firts object [a]first object and then stops.

                • 5. Re: Looping through an array more that once
                  dharmk Adobe Employee

                  Hi Charine,

                   

                  you have missed the mod operator in the actual calls.

                   

                  lettersL1[i].x = 399;

                  lettersL1[i].y= 30;

                  addChild(lettersL1[i]);

                   

                  You can use the following:

                   

                  for (var i: Number  =0; i < lettersL1.length*3; i++)

                  {

                            var index = (i%lettersL1.length);

                            trace (lettersL1[index]);

                   

                            lettersL1[index].x = 399;

                            lettersL1[index].y= 30;

                            addChild(lettersL1[index]);

                  }

                   

                  Since there are only lettersL1.length number of elements in the array, the moment the value of i goes beyond the limit you'll not be able to retrieve any elements at that index from the array. Having an updated index ( i % lettersL1.length) fixes this issue.

                   

                  Regards,

                  Dharmendra.

                  • 6. Re: Looping through an array more that once
                    Charine -FB buggoop Community Member

                    Hi Dharmendra,

                     

                    Thanks, this works like a charm. Thanks for the nice explination too. This way I learn coding faster as I go.

                     

                    Have a great day

                    Charine