- Home
- Tutorials
- Flash
- Intermediate
- Scripted Loops

Examples and applications of loops
Jesse Stratford
Jesse lives and works in Melbourne Australia. He is the Cofounder and a Director of http://ActionScript.org. A Flash enthusiast, teacher, author, freelancer and speaker Jesse enjoys participating in the http://ActionScript.org community and the wider Flash scene when he has time.
View all articles by Jesse Stratfordfor (x=0; x<100; x++) {
duplicatemovieclip ("targetMC", "dupMC"+x, x);
}
(I assume you can understand most of that, having read the Beginner level tutorials like a good student would, the one bit you might be unsure about is this part: "dupMC" + x, it just takes the string "dupMC" and concatenates (appends to the end) the number stored in the variable x to it, so if x = 1, it would return "dupMC1". Since x is ever changing this will give a unique name each time).
Of course, having read the duplicatemovieclip tutorial you would know that all these new duplicates wont be visible because they will all have the same _x and _y, so we need to move them around a bit. But that's not a problem either with loops. No we don't have to type out 100 _x and 100 _y coordinates, we just include a clause with some sort of algorithm in our loop. Say we want to make each new duplicate have the same _y but an _x that is 5 pixels greater than the previous duplicate (to give the effect of tiling this graphic horizontally). We just do this:
for (x=0; x<100; x++) {
duplicatemovieclip ("targetMC", "dupMC"+x, x);
_root["dupMC"+x]._y = x*5;
}
Note that the only addition here is line 3 and it's not that difficult. Those of you who have read the Set Property tutorial will know that using the setProperty command we can move movie-clips around on the stage like points on a grid. This is exactly what I'm doing in Line 3 but I'm just using the 'infix path' notation. So instead of using the old-fashioned "setProperty(property, target, value)" notation, I simply give the path to the target and then append "._y" which indicates I want to work with the Y position of this target. Then I am setting the Y position of the target to the current x value times 5. So the first duplicate will have an _x of 0 (because x starts at 0 and 0 * 5 = 0), the next will have an _x of 5 and the last will have an _x of 495 (not 500 because we're looping while x is less than 100, not less than or equal). Note also that in this line I use the 'array method' for determining the name of the clip I wish to set the property of. We have just duplicated a new clip and given it a dynamic and unique name which we created by adding the value of x to the string "dupMC". Therefore I need to tell Flash that I want to set the property of the MC with that name. That is done as above, not via _root.dupMC+x._y = x * 5 as a lot of people assume. The square brackets basically tell Flash that it must calculate the value of what is within them and sub it into that line. Finally, note there is no dot between the "_root" and the open square bracket in the array method.
Phew! What a huge paragraph and it's not even related! Oh well, at least I can use the array method from here on in and you will all understand what I'm on about.
Sticking with a similar example, what if you have a text field that you want a user to enter a numerical value into, and upon clicking a button that many duplicates of a movie clip will be produced? Can anyone guess how we're going to approach that? "With loops Sir". That's right, with loops.
So you have a text input field which takes the variable count. Then you have a button with this code:
on (release) {
for (x=0; x<Number(count); x++) {
duplicatemovieclip ("targetMC", "dupMC"+x, x);
_root["dupMC"+x]._y = x*5;
}
}
What's changed here you say? Look at line 2. No longer are we performing our loop for x<100. Remember, we want to make only as many duplicates as the user asked for. So now we want x to start at 0 and iterate until it's less than Number(count). The Flash Number() function takes a String which contains a number, and converts it to a number. Having read the tutorial on Variables you would know we cannot perform Math operations on a String and you would also know that values entered into a text field are Strings by default, so we need to convert this String to a Number before we check if it's greater than x.
Last but not least remember that scripted loops cannot be used to modify visual events over a period of time. What's that mean? It means you can't use a loop to try and animate a graphic moving across the screen, or to increment a counter that you want to display the value of. Why not? Because loops are executed faster than you can blink (quite literally). Often we hear people complaining that they have created a loop in which they want the _x of a MC to increase 100 times and "Flash just isn't doing it". Wrong! Flash is doing it, you just can't see it because it's happening so quickly! Also keep in mind that all events in a loop are executed before any lines of code after that loop and before moving on to the next frame in the timeline! Thus loops can slow your movie down and cause your audio to fall out of sync, and all sorts of trouble if you don't manage them appropriately.
So that's about it. If anyone can think of any more practical examples or anything I've left out, as always, please email me. Also email me if you have a comment or suggestion about the tutorial. However, if you have questions post them on the forums (I'm going to start writing that in hyper-color bold at size 500 :o)
| Jesse Stratford is the Co-Master of ActionScript.org and a freelance Flash developer and teacher. He is based in Australia and enjoys all things Flash. NB: If you have comments or feedback please feel free to email me, but please do not email me Flash questions; the forums are provided for that purpose and you will get a faster answer by posting you question there. |
If you have found this tutorial helpful, I hope that you will take 30 seconds to visit The Hunger Site where, with just one click you can make a free donation of food to a starving person in a third-world country. We do not benefit financially from this action; it is purely an act of charity. |
| This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution. I've worked hard on this tutorial, please don't steal it. |


