Skip navigation
Charine -FB buggoop
Currently Being Moderated

Looping through an array more that once

Jun 26, 2013 6:46 PM

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

 
Replies
  • kglad
    72,198 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 26, 2013 8:39 PM   in reply to Charine -FB buggoop

    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]);                  

                    }

     


     
    |
    Mark as:
  • Currently Being Moderated
    Jun 27, 2013 9:36 AM   in reply to Charine -FB buggoop

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

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 27, 2013 9:54 AM   in reply to Charine -FB buggoop

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points