ActionScript For Loop Code - The Code

ActionScript For Loop Code - The Code
var _Snowflake:mcSnowflake;
This first line of code will create a generic variable called "_Snowflake" that will act as a placeholder for each instance of the movie clip as it is added. It also tells Flash to use our class name "mcSnowflake" as the data type for the empty placeholder variable.

for (var i:Number = 0; i < 10; i++)
The next few lines of code is a code loop that we will use to "drag" several instances of the movie clip to the stage. The for at the beginning of the code tells Flash that this is a for loop and that we want to do something "for a certain number of times". Now, let's take a look at what is between the parentheses.

var i:Number = 0;
This line of the code loop creates a variable called "i" that will be our counter. We have set the data type to Number and the initial value to 0. The semicolon ends this part of the code.

i < 10;
This code tells Flash to repeat the task as lone as the counter is less than 10. This will create 10 instances of the mcSnowflake movie clip and number them 0 through 9. Again, a semicolon ends this part of the code.

This small piece of code will increment the counter by 1 each time the loop is repeated.

Next, we will tell Flash exactly what to do each time it performs the for loop. These instructions are placed between curly brackets.

_Snowflake = new mcSnowflake();
_Snowflake.x = i * 50;

_Snowflake = new mcSnowflake();
This will create a new instance of the mcSnowflake movie clip. Remember that _Snowflake is our placeholder for each instance.

The addChild method is a built-in function that will make the movie clip visible on the stage by adding it to the display list.

_Snowflake.x = i * 50;
This code will place each instance of the movie clip 50 pixels apart. If we did not add this code, Flash would place all ten of the movie clips, one on top of the other, at the default position of 0,0 which is at the top left corner of the stage.

When you test your movie, you should have 10 flakes placed along the top of the stage. That's better than our last version but we still need to make the flakes fall vertically down the stage. Also it would be nice to randomly change the size of the snowflakes and add each instance at a random location on the stage. Of course, we need more then just 10 flakes. We will be learning how to do all of this in the rest of the series. For reference, save your snow scene as SnowScene2.fla.

← Back

Copyright 2018 Adobe Systems Incorporated. All rights reserved. Adobe product screen shot(s) reprinted with permission from Adobe Systems Incorporated. Adobe, Photoshop, Photoshop Album, Photoshop Elements, Illustrator, InDesign, GoLive, Acrobat, Cue, Premiere Pro, Premiere Elements, Bridge, After Effects, InCopy, Dreamweaver, Flash, ActionScript, Fireworks, Contribute, Captivate, Flash Catalyst and Flash Paper is/are either [a] registered trademark[s] or a trademark[s] of Adobe Systems Incorporated in the United States and/or other countries.

Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map

Content copyright © 2021 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact Diane Cipollo for details.