easy to understand your tutorial, thank you so much !
Tutorial 3 ? the loops
Now that we saw what are Arrays and how to use them I carry on with the next step of the basics of Actionscript 3.0.
In this article we will see what are the loops, the use of them, the different types of loop and how to use them.
Besides, the loops allow us to go through every element of an Array using the variable of control of the loop as index of the Array.
The loops are blocks of code that are repeatedly performed up to when a certain condition (called condition of loop) has a true value, meaning the condition is right.
Actionscript 3.0 has 5 types of loop available?
for ( the mostly used )
while
do - while
for - each - in
for - in
Let us see how to use them?
for loop
This loop is normally the mostly used and the easiest one to learn for those of you who are starting with Actionscript 3.0.
A ?fo?r loop is a structure which allows to repeat some actions from zero to a specified number declaring the number of iteration (the number of times the code will be performed). The loop could also be an infinite one.
To create a ?for? loop, there are 5 fundamental rules to follow:
1. Declaring the word ?for? so that Flash understand we are starting a loop
2. Declaring a variable of control
3. Declaring a condition of loop
4. Declaring an expression to increase the initial value
5. declaring a block of instruction to carry out
So, with Actionscript 3.0, we declare a ?for? loop the following way:
We obtain the following outputCode:for(var i:int=0;i<10;i++) { trace(i); }
0
1
2
3
4
5
6
7
8
9
for , I declare the loop
var i:int=0; I declare a variable of control (with an initial value of 0 for this example)
i<10; the condition for the loop. The loop will loop the time that condition is true
i++; the expression to increase the variable of control. At each loop, the variable is increased of one
trace(i); the block of instruction which will be carry out at each iteration of the loop
Briefly this is what happens:
? Flash recognised that it is a loop thanks to the operator ?for?
? It carries out the block of instruction placed in between the brackets
? As it is a loop, Flash won?t carry out any code placed after the ?for? loop but will loop once again and increase the variable of control
? It checks if the condition is true or not. If it true, it will loop through the block of instruction once again
? When the condition is false, Flash will carry on with the rest of the code placed after the ?for? loop as you can see in the following example:
output:Code:for(var i:int=0;i<10;i++) { trace(i); } trace('fatto');
0
1
2
3
4
5
6
7
8
9
fatto
As I have already explained the Arrays of Actionscript 3.0, we can now see how to ?navigate? them and access their elements using a ?for? loop.
I create an ArrayCode:var nomi:Array=new Array('filippo','andrea','giovanni','pietro','giacomo','luca','paolo'); for(var n:int=0;n < nomi.length;n++) { trace(nomi[n]); }
var nomi:Array=new Array('filippo','andrea','giovanni','pietro','giac omo','luca','paolo');
I create a for loop with as a condition the maximum value of the array?s length (using the property length of the Array Class seen in the second tutorial about the Arrays)
for(var n:int=0;n < nomi.length;n++)
I then use the variable of control (n in this example) to access every single elements of the Array via its index
trace(nomi[n]);
I obtain the following output
filippo
andrea
giovanni
pietro
giacomo
luca
paolo
while loop
The structure of a ?while? loop is very similar to a conditional logic (if) that we will see further on.
As before, this loop allows to repeat part of a code more times starting from zero declaring a number of iteration.
The difference with the ?for? loop is that the ?while? loop is declared the following way:
variable of control
while(condition of loop)
{
block of instruction
expression to increase
}
so
gives the following outputCode:var j:int=0; while(j<10) { trace(j); j++; } trace('fatto');
0
1
2
3
4
5
6
7
8
9
fatto
as we can see, the result is the same as with the ?for? loop, the type of structure and the implemented logic of our application will tell us which type of loop is best to use.
Also, it is important to notice that the action trace(?fatto?); is not performed until the condition of the loop do not result false.
do while loop
This loop could be defined as a reversed version of the ?while? loop.
Its structure is as follow:
variable of control
do
{
block of instruction
expression to increase
}
while(condition of loop);
so
with the following outputCode:var k:int=0; do { trace(k); k++ } while(k<10); trace('fatto');
0
1
2
3
4
5
6
7
8
9
fatto
Once again, in this case, the output does not change. The final result is the same as with the ?for? or ?while? loop. The condition of the structure of our application that we are developing will tell us which is the best type of loop to use (I will repeat myself saying that in 90% of the case, the ?for? loop is the one used).
for each in loop
This loop is fundamental to access the dynamic properties of an object or the elements of an Array via their index.
The structure is as follow:
for each(variableOfControl in Object)
{
block of instruction
}
So, let us say to have an object with more properties:
with a ?for each in? loop we can retrieve the properties of the object the following way:Code:var oggetto:Object=new Object(); oggetto.nome='filippo'; oggetto.cognome='lughi'; oggetto.sesso='maschile'; oggetto.paese='cesenatico';
and obtain the following outputCode:for each(var p in oggetto) { trace(p); }
filippo
lughi
maschile
cesenatico
Adobe do not guarantee the order of the output other that in the case we are working with the XML and XMLList Class.
In fact, if we add a property to object of numerical type
and obtain the following outputCode:var oggetto:Object=new Object(); oggetto.nome='filippo'; oggetto.cognome='lughi'; oggetto.eta=35; oggetto.sesso='maschile'; oggetto.paese='cesenatico';
filippo
35
lughi
maschile
cesenatico
The following code shows a for each loop into an Array:
and obtain the following outputCode:var nomi:Array=new Array('filippo','andrea','giovanni','pietro','giacomo','luca','paolo'); for each(var s in nomi2) { trace(s); }
filippo
andrea
giovanni
pietro
giacomo
luca
paolo
for in loop
This loop has the same structure as the ?for each in? loop. The unique difference is that the ?for each in? loop goes through the value of the variable when in the ?for in? loop, it retrieves the names of the variable.
Let us take a look at an example using the same Object as before:
and obtain the following outputCode:var oggetto:Object=new Object(); oggetto.nome='filippo'; oggetto.cognome='lughi'; oggetto.sesso='maschile'; oggetto.paese='cesenatico';
nome
eta
sesso
paese
cognome
I would like to remind to those of you at the beginning with Actionscript 3.0 to exercise a lot and reach a good use of the loops.
Source files:
Last edited by Flep; 05-06-08 at 19:09.
easy to understand your tutorial, thank you so much !
Bookmarks