Advertisement |
Basic Loop Usage
If it wasn't for loops you would have to repeat a statement multiple times if you want to have an action executed more than once. For example, say that you want to create ten Movie Clips and add them to the display list. The long way for doing this would have been to type the command ten times:
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
However, you can do the same exact thing using a loop without having to type the same code multiple times:
for (var i:Number=1; i<=10;i++){
addChild(new MovieClip());
}
addChild(new MovieClip());
}
This second code generates the same exact result, but in a much more compact way. Using a loop also makes it less likely for you to have typing errors because you type the code once and then it is automatically repeated.
There are five different loop types, we are going to explain how to create each of them in turn.
click here for loop
click here for loop
0 comments: