Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Using An Actionscript In Flash For Loops
Sharkyx
post May 17 2006, 06:19 PM
Post #1


Newbie
*

Group: Members
Posts: 1
Joined: 17-May 06
Member No.: 23,828



QUOTE
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:
CODE
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
CODE
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
CODE
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
CODE
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.
CODE
for(var i=1; i<=50;i++){
     trace(i);
}


[Post found to be copied - OpaQue]
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. What is the best USB Flash Disk outhere?(9)
  2. Balloon Duel - Flash Game(2)
  3. Flash Games...(8)
  4. Delay X Seconds In Flash(1)
  5. Ie And Firefox Crash(5)
  6. How To Get High Quality Gif Images In Flash(1)
  7. Playing Flash Movies Without The One-click Activation: Simple Insertion Of Javascript(4)
  8. Toufee.com - Create Free Flash Movies Online(3)
  9. Virus On Your Flash Disk? (usb Devices)(15)
  10. Flash Actionscript Help(10)
  11. How To Create A Song In Fruity Loops Using Vanguard(1)
  12. Fruity Loops Tutorials-trance Style Songs(11)
  13. Photoshop, Flash And Fireworks Tutorial Site Links(2)
  14. I Need Some Help With Flash And Div Overlay(5)
  15. Flash As3 Question(2)
  1. Scroll And Pan The Screen Via Mouse In Flash(1)
  2. Cms For Video, Embedding Flash(2)
  3. Flash, Php And Facebook Integration(0)
  4. Adobe Flex/flash/actionscript Forum(1)
  5. Essential Actionscript 3.0 Book Review(0)
  6. Which Flash Drive Do You Use?(26)
  7. Flash Header Edit?(2)
  8. Flash Tutorial Simple Motion Tween(0)
  9. Flash And Transparency(7)
  10. Free (legal) Flash Editor? Anyone?(9)
  11. Actionscript 3(0)
  12. Flash Video Recorder(2)
  13. Flash Action Scripting Help Needed(6)


 



- Lo-Fi Version Time is now: 12th October 2008 - 07:40 AM