Looping “for” more

Have you ever come across a situation, when you need to do the same work all over again ? Well, the same thing holds good for flash too. Sometimes we need certain things to keep happening. In that situation Flash helps us with a nifty method called as "for" loop. there are other loop methods too.But here we will concentrate on "for" loop.
Why it is called as a "loop"? Simply because it does the same thing again and again.
Open a new flash document and type in the following script on the first frame
for(item=0;item<10;item++){
trace("item : "+item);
}
Now test your movie. You will see flash is puting values one after the other 10 times starting at 0. This is because we set a condition in the "loop" as "item<10".
How it works? First of all we say a staritng point for the loop as (item=0), you can take any variable of your choice. Generally it is taken as "i" . Then we define upto what point the loop should continue (item<10). and lastly we provide how our variable will be incremented (item++), that means here our item is increased by 1 each item one loop is done. Lets try the same loop with different parameters and writing in best practices' way

for(var i=0;i<10;i+=2){
trace("i : "+i);
}

First we declare a variable as
var i=0;

Then we set the condition as
i<10;

Then we said how "i" will be incremented as

i+=2
that means "i=i+2" .Lets write that way

for(var i=0;i<10;i=i+2){
trace("i : "+i);
}

But this is little unconventional and long so its generally written the former way.

Our "for loop" till now does nothing but just outputting the value of "i".Now lets write a loop that will call a function.
function myFunct(item:Number):Void{
trace("item : "+item);
}
for(var i=0;i<10;i+=2){
myFunct(i);
}

If you test your flash movie, you will see that it returns the same value as our preveous code.But we are actually sagregating our script and our loop. Here our loop is calling the function "myFunct". If you look at "myFunct" function then you will see that it simply takes one number value and then traces it out. Now in our "for loop" we are providing that function with value "i".
Lets modify our script as
function myFunct(item:Number):Void{
trace("function called");
trace("item : "+item);
}
for(var i=0;i<10;i++){
myFunct(i);
}

See the results in the output panel. Thats pretty expected ones.

Happy looping 😉

Advertisements

Author: saumya

designer / developer / maker / educator @saumya

1 thought on “Looping “for” more”

  1. I have probably been looking around online for like the last 20 minutes for a site like yours. So i just want to give you a big thank you and tell you too keep up the hard work. Have a great weeken! =) Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s