Monday, September 17, 2018

The "For" Loop for Actionscript 3

ad300
Advertisement

The "For" Loop

The for loop is most widely used loop format because of its very compact form and because it is the easiest one to understand and use. You can use use this loop to execute a number of code statements multiple times using this format:
for (counter; condition; action){
statements;
}
Each for loop must have a counter (also called an iterator). This counter can be any number variable. You can define this counter when you create the loop:
for (var i:Number=0; condition, action){
statements;
}
Or alternatively you can define the counter before hand and just reset it when you want to use it in the loop:
var i:Number;for (i=0; condition, action){
statements;
}
You can learn more about  by reviewing our tutorial on this topic.
The second element in the loop is a condition. The loop will only run while that condition is satisfied. For example, if you want your loop to run ten times you will need to state that i must not be greater than ten:
for (var i:Number=0; i<10 , action){
statements;
}
Finally, you have to specify the action by which i will increase, decrease, or multiply. In most simple cases you would have this value increase by one. This command is executed after each loop cycle.
for (var i:Number=0; i<10 , i++){
statements;
}
The action you set to i must eventually unsatisfy the condition, so in our example, by increasing the value of i we will eventually make it not less than 10. If your loop definition does not eventually fail the condition, your will have an infinite loop that will cause the player to crash.
Your loop has now been defined and you can put any code inside it to have play repeatedly.
for (var i:Number=0; i<10 , i++){
new MovieClip();
}
It is very common to make use of the i counter inside the loop. As the value of i will increase by 1, it is usually helpful to have each of your objects have a unique name by the value of i in its name. For example, if we wanted to have our MovieClips have their names in a series the series of mc1, mc2, mc3, etc. We would do that by using i as part of the name this way:
for (var i:Number=0; i<10 , i++){
var my_mc = new MovieClip();
my_mc.name = "mc"+i;

addChild(my_mc);
}
You cannot make use of variables created inside a loop and must use other methods for creating references to objects from inside the loop. You can learn more about this from the last section of this tutorial on variables and loops.
The code above will create ten MovieClips, name them in the series of mc1, mc2, mc3, etc, and add each of them to the display list. Click for while loop
That is basically everything you need to know to start using the for loop. You might want to check our other tutorials to see how it used in actual projects.
Share This
Previous Post
Next Post

Pellentesque vitae lectus in mauris sollicitudin ornare sit amet eget ligula. Donec pharetra, arcu eu consectetur semper, est nulla sodales risus, vel efficitur orci justo quis tellus. Phasellus sit amet est pharetra

0 comments:

Ad Section2

Sponsor

Ad Section