Hey Wuup readers. After going through all my older AS3 Basics articles, I found that most of them didn’t really go over the core elements. So in order to rectify the problem, I’ve decided that it’s time to flesh out this area a little. I’m by no means an expert on the language – unless you count 3 years experience as expert material – But I do know what does what, where it goes, and how it does it. Read on.
So, firstly, what is a loop?
A loop is a conditional statement which repeats given instructions until some pre-defined conditions are met.
Consider this pseudocode:
do this stuff
{
paint fence.
}
while (fence is not painted)
Here, we’re telling the loop to paint our fence while it is not painted. This is the basic principal behind all loops.
There are 4 types of loop in AS3:
- For
- For In
- For Each In
- Do While
The example we looked at is the 4th type, a Do While.
Lets have a look at what is possibly the most common, the ‘For’ loop.
For Loop
This code will count up to 100:
for(var i = 0; i < 100; i++)
{
trace(i);
}
So for the given value, if the given value is less than 100, increment given value, repeat. You may notice that this code will actually count to 99 in flash, This is because we have started at an initial value of 0.
The process for a For Loop can be show in this graph:
Notice how the code within the brackets is executed before our value is incremented.
In the next article i’ll explain about the for loops cousin the Do While.
Hope this helped
Remember to stay well chilled
-Bill




