How and why to use a Macromedia Flash Actionscript for loop


posted: 06-09-05
author: PrimeVector
website: http://www.free-webmaster-resource.com
views: 64451
comments: 9
total pages: 1
jump to comments


Amazing Webmaster Widgets





Using an Actionscript for loop

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

jump to comments


Amazing Webmaster Widgets




Comments

9 comment(s) found in 2 pages  
viewing comment(s): 1 - 6
page: 1 

1 | 2 |

23-01-07:guest

show me some small flash animation example using the for loop

08-01-07:guest

i dont understand it can u tell me frm begining.how to create this starry effect using actionscript.

12-07-06:guest

I found this very helpful! Very easy to understand and well written, thank you so much!

20-02-06:primevector

Please refer all questions to the forums ... There we can work on making you understand it too.

19-02-06:R5

I dont understand wath is this

12-12-05:guest

cool



- Press here to view all available smilies!





In Focus
Cool Stuff