How and why to use a Macromedia Flash Actionscript for loop


posted: 06-09-05
author: PrimeVector
website: http://www.free-webmaster-resource.com
views: 48887
comments: 9
total pages: 1
jump to comments


Amazing Webmaster Widgets





Using an Actionscript for loop

When you are dealing with a lot of data, you without a doubt will run into the need to use loops. Lets say you would want to attach a movieClip to the stage. If its just a few clips it won't form a problem, but what if you wanted to attach a lot more clips?

It would be impossible to set them up piece by piece. It would take hours of coding the same code over and over again and that redundancy is something you could really do without, not even talking about the time you are loosing. This is where loops come into action. We have a few different ones to our disposal but in this tutorial i will address the 'for' loop.

To stay with the attach example, lets say i wanted to attach 5 movieclips from the library to the stage. Without a loop i would have to do something like this:

attachMovie("myMovie_1","myMovie_1",1);
attachMovie("myMovie_2","myMovie_2",2);
attachMovie("myMovie_3","myMovie_3",3);
attachMovie("myMovie_4","myMovie_4",4);
attachMovie("myMovie_5","myMovie_5",5);


As you can see this piece of code is very in-efficient, we keep repeating nearly the same line of code 5 times over. This isnt good, so lets setup the same thing but this time using a 'for' loop. What this for loop does is it sets up a variable called 'i' with a start vaule of 1. As long as variable 'i' is smaller or equal to 5 it will increase like 1,2,3,4,5

for(var i=1; i <= 5 ;i++){
     attachMovie("myMovie_" +i, "myMovie_"+i, i);
}


Doing it this way you could setup as many clips to the stage as you want to. As you can see now we only have to use the attachMovie 1 time but because its setup inside the for loops brackets {} its executed 5 times. the variable "i" we setup in the loop to start at number 1 will render to 1,2,3,4,5 inside the loop. So this line attachMovie("myMovie_" +i,"myMovie_"+i,i); will render to

attachMovie("myMovie_" +1,"myMovie_"+1,1);
attachMovie("myMovie_" +2,"myMovie_"+2,2);
attachMovie("myMovie_" +3,"myMovie_"+3,3);
attachMovie("myMovie_" +4,"myMovie_"+4,4);
attachMovie("myMovie_" +5,"myMovie_"+5,5);


which itself renders to

attachMovie(myMovie_1,myMovie_1,1);
attachMovie(myMovie_2,myMovie_2,2);
attachMovie(myMovie_3,myMovie_3,3);
attachMovie(myMovie_4,myMovie_4,4);
attachMovie(myMovie_5,myMovie_5,5);


So you see with a loop you can save yourself loads of time.

Below is another quick example loop that traces the the value of 'i' to the screen. So you'll see 1 2 3 4 5 etc... up to 50. The <= means 'smaller then or eaqual to', so in this example the 50 is also displayed.

for(var i=1; i<=50 ;i++){
     trace(i);
}

jump to comments


Amazing Webmaster Widgets




Comments

9 comment(s) found in 2 pages  
viewing comment(s): 1 - 6
page: 1 

1 | 2 |

23-01-07:guest

show me some small flash animation example using the for loop

08-01-07:guest

i dont understand it can u tell me frm begining.how to create this starry effect using actionscript.

12-07-06:guest

I found this very helpful! Very easy to understand and well written, thank you so much!

20-02-06:primevector

Please refer all questions to the forums ... There we can work on making you understand it too.

19-02-06:R5

I dont understand wath is this

12-12-05:guest

cool



- Press here to view all available smilies!



Latest News

Amazing Webmaster Tool

If you run a MySpace or teens related site with visitors interested in GLITTERS you will need this great tool: Webmaster Glitter Tool It will generate backlinks at an amazing pace!

 

Useful Click To Clipboard Script

This is a nice little copy to clipboard script. You can find it here!

 

Google Pages

Create pages on google quick and easy with Google Pages!

 

My Funny Pictures Site

Have fun on this funny pictutures site i made, i hope youll enjoy it :) PixHumor

 

IS YOUR CHILD SAFE???

With this great program you can make sure your child will be a lot safer while browsing the Internet.

 

New Site

I created a fun site for MSN Messenger Stuff called MSN Freak. You can find the site here: Free Winks @ MSN Freak

 

Useful free unlocker tool

This tool is a very useful tool that runs in the background. Whenever you try to for example copy a folder and explorer sceams at you it wont do that, the barrier is lifted and you can continue to do what you wanted too. Download it here.

 

GOOGLE worldwide: Languages and countries

Useful page showing all the Google Search Engines of all countries. Check it out here.

 

Google Trends

Google trends enables us to see where people want certain things the most. You can enter a keyword and find out where people search for that most often! Check Google Trends

 

Google PageRank Checker

I personally enjoy this Google pagerank checker a lot: Google PageRank Checker

 

New tutorials

I added a new basic Flash tutorial showing how to create a simple motion tween. You can find it here: Basic Flash Motion Tween

 

2 new Flash tutorials

I added 2 new Flash tutorials to the tutorial selection. On deals with the if else conditional statement and the other deals with setting up a bit of color with actionscript. You can checkout the tutorials here: Actionscript and colors and If Else Conditionals.

 

Oh My!

Its mad times overhere, im helping promote and have created a few sites which has taken quite some time from me. site1: http://www.flashgamefreak.com, site2 http://www.freewinks.net, site3 http://www.smilieworx.com and more are to come this and next week. This has taken its toll on my time to write tutorials. I will make up for that as soon as possible. Thanks for your understanding.

 

Basic Flash Tutorials

I added 3 new basic Flash tutorials to the tutorial list. Dealing with layers, What are functions and one about The Registration Point.

 

A bit of a delay

I have had a very crazy time around here the last month, because of this some tutorials have not been written yet. This is a list of tutorial i still need to write and you can expect them in the very near future.

 

Advertise on FWR

If you want to advertise here then please contact me here. We can make a nice deal and you can get stats on them how often you would like.

 

2 new tutorials

2 new web design related tutorials were added ... more Flash tutorials are on their way but its a bit mad around here these last days ;)

 

View All News



In Focus

Play Frogger Now!
Play frogger with 1 click download of gamevance!
Play Airstrike 1941 online for free!
Play Airstrike 1941 online for free!
Free Auto Insurance Quotes!
Saving money with InsureMyCar4Less is simple. After you fill out our quick online form, you get free auto insurance quotes from the best providers in the industry. They compete for your business, and you save money.
Play World War 2 Commando Free!
Play World War 2 Commando online for free!
Do you shop at Home Depot?
Get a ton of Home Depot coupons and gift card offers! Also if your looking to improve you home this is the place to be!
Make cash with Penny Stocks!
Make cash with Penny Stocks!
Play Aztec Quest online for free!
Play Aztec Quest online for free!
Get a Free Auto Insurance Quote
Free quotes from top insurance carriers!
Play Swat 2 Now
Play the swat 2 game and shoot those bad guys!
Play Bubble Boomers
Pop the bubbles of the like colors for this very fun and addictive game for FREE!!!
Customize your Facebook with a theme
Customize the colors and music on your facebook profile page today!
Print Coupons from Home!
Print Grocery Coupons from your house! Instant Savings!
Get free Auto Insurance Quotes!
Saving money with InsureMyCar4Less is simple. After you fill out our quick online form, you get free auto insurance quotes from the best providers in the industry. They compete for your business, and you save money.
Stop identity theft in its tracks!
Protect My ID makes it easy for you!
Save up to 70% on auto insurance!
Do you qualify for 60-70% off your auto insurance rates? Find out right now! Request quotes 24 hours a day with our secure online form. It's super fast and easy. See what your rate is absolutely free!
Play Streetfighter Now!!!
Get all the hottest new games and all the old school games in one place!
Play Pac Man right here!
Get all the hottest new games and all the old school games in one place!
Play the old school Baseball Stars
Get all the hottest new games and all the old school games in one place!
Dont get your identity stolen!
we offer multiple layers of protection and are backed by experian. Start with only $1!
Share your Opinion, win $50,000?
Share your Opinion, win $50,000? Couldn't get any easier! It's free and fun!

Cool Stuff