advertisement

Hi, sorry it’s been a while since it wrote a tutorial. Hopefully in this tutorial I will teach you what a for loop is and how to apply them to flash with actionscript. On with the tutorial…

Basically a for loop is something that holds some code and the code inside the loops is repeated x amount times. For example you could use a for loop to repeatedly send a spam email to someone (but we wouldn’t want to do that would we?). Anyway… in the example that I will show you, we will be attaching movie clips and then counting how many clips have been generated with actionscript.


Ok, first thing, fire up flash, create a new file with 550×400 (default) dimensions. Now draw a circle on the stage with the oval tool, make sure it’s height and width are 25.5. Select the circle and convert it to a symbol (F8), make it a movie clip and give it a name of circle. Now select the movie clip of circle and delete it (make sure you don’t delete it from the library, just delete it from the stage). Now open your library up (Ctrl+L) and you will see a little rectangle where your circle symbol is:
circle movie clip
right click that and it will come up with a bar like this:
right click bar
You will want to select “Linkage…”. Then it will come up with a box like this:
linkage box
All you will want to do is tick the “Export for ActionScript” box and it will do the rest (Important – Make sure the identifier is set to “circle” (without quotes)). Then press OK.

*  *  *
Text Box Setup
*  *  *

Now create a Dynamic Textbox at the bottom of the stage about the width of the movie (I used text size 22 with centered text), and give it a var of “numtxt” without quote marks. Make sure the text box has a minimum y value of 366.0.

*  *  *
Actionscript
*  *  *

Now open the actions panel (F9) and first of all, add this:
[as]
stop();
[/as]
That just stops the movie. Now add this
[as]
var num = Math.random () * 1000;
[/as]
This just creates a variable called “num” that is equal to a random number between 1 and 1000.
[as]
onLoad = function() {
[/as]
This calls a function when the movie is loading. Now add:
[as]
for (i = 0; i < num; i++){ [/as] This creates a for loop in which i is equal to 0, and when i is less that num (num is the variable set earlier on), run the code in the loop. [as] var att = attachMovie ("circle", "circle"[i], i); [/as] This creates a variable that attaches the circle to the movie giving it a new name which is circle[i], remember this is inside the loop so this code will attach a circle as many time as the loop is run. Now add this: [as] att._y = Math.random () * 350; att._x = Math.random () * 550; [/as] These two lines of code just randomize the x and y values of the circle(s) attached, Note: The y value only goes up to 350, this is because if your text box's y value is 366.0 you won't be able to see the text in the box because circles will be covering it up. Add: [as] } }; [/as] Just closing the for loop and the function. Now add: [as] onEnterFrame = function (){ [/as] This calls a function when the movie enters this frame, run the code inside... Add: [as] _root.numtxt = "Approximateley " + Math.round (num) + " circles have been dynamically generated."; [/as] This makes the text box with the var of numtxt equal a string ("Approximateley ") and the num variable created earlier (which has been rounded using "Math.round()") and another string. If you dont want the rounded number, you want the EXACT number, find: [as] Math.round (num) [/as] and replace with: [as] num [/as] And that is about it... The full code from this tutorial is: [as] stop (); num = Math.random () * 1000; onLoad = function () { for (i = 0; i < num; i++) { var att = attachMovie ("circle", "circle"[i], i); att._y = Math.random () * 350; att._x = Math.random () * 550; } }; onEnterFrame = function () { _root.numtxt = "Approximateley " + Math.round (num) + " circles have been dynamically generated."; }; [/as] I hope you understand for loops a bit better now... If you have any questions, feel free to leave comments for me. An example of what we make in this tutorial is here. Just refresh the page for it to generate a different amount.

Thanks for reading,
Mortisimus
http://www.flashdoom.net/

advertisement