Jump to content
xisto Community
Sign in to follow this  
Sharkyx

Using An Actionscript In Flash For Loops

Recommended Posts

When you are dealing with a lot of data, you without a doubt will run into the need to use loops. Lets say you would want to attach a movieClip to the stage. If its just a few clips it won't form a problem, but what if you wanted to attach a lot more clips?
It would be impossible to set them up piece by piece. It would take hours of coding the same code over and over again and that redundancy is something you could really do without, not even talking about the time you are loosing. This is where loops come into action. We have a few different ones to our disposal but in this tutorial i will address the 'for' loop.

To stay with the attach example, lets say i wanted to attach 5 movieclips from the library to the stage. Without a loop i would have to do something like this:

attachMovie("myMovie_1","myMovie_1",1);attachMovie("myMovie_2","myMovie_2",2);attachMovie("myMovie_3","myMovie_3",3);attachMovie("myMovie_4","myMovie_4",4);attachMovie("myMovie_5","myMovie_5",5);

As you can see this piece of code is very in-efficient, we keep repeating nearly the same line of code 5 times over. This isnt good, so lets setup the same thing but this time using a 'for' loop. What this for loop does is it sets up a variable called 'i' with a start vaule of 1. As long as variable 'i' is smaller or equal to 5 it will increase like 1,2,3,4,5
for(var i=1; i <= 5;i++){	 attachMovie("myMovie_" +i, "myMovie_"+i, i);}

Doing it this way you could setup as many clips to the stage as you want to. As you can see now we only have to use the attachMovie 1 time but because its setup inside the for loops brackets {} its executed 5 times. the variable "i" we setup in the loop to start at number 1 will render to 1,2,3,4,5 inside the loop. So this line attachMovie("myMovie_" +i,"myMovie_"+i,i); will render to
attachMovie("myMovie_" +1,"myMovie_"+1,1);attachMovie("myMovie_" +2,"myMovie_"+2,2);attachMovie("myMovie_" +3,"myMovie_"+3,3);attachMovie("myMovie_" +4,"myMovie_"+4,4);attachMovie("myMovie_" +5,"myMovie_"+5,5);

which itself renders to
attachMovie(myMovie_1,myMovie_1,1);attachMovie(myMovie_2,myMovie_2,2);attachMovie(myMovie_3,myMovie_3,3);attachMovie(myMovie_4,myMovie_4,4);attachMovie(myMovie_5,myMovie_5,5);
So you see with a loop you can save yourself loads of time.
Below is another quick example loop that traces the the value of 'i' to the screen. So you'll see 1 2 3 4 5 etc... up to 50. The <= means 'smaller then or eaqual to', so in this example the 50 is also displayed.
for(var i=1; i<=50;i++){	 trace(i);}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.