ActionScript For Loop Code
At this point, we have our SnowScene1.fla file that contains three layers in the Timeline. The Background layer contains the background image. The next layer, called Snowflakes, contains three instances of the mcSnowflake movie clip from the Library. The top layer is the Actions layer where we have our ActionScript.
But, three snowflakes aren't enough for our snow scene. We will need many more instances of the mcSnowflake movie clip to create the appearance that it is snowing in our scene. We could continue to drag instances from the Library and manually give each a unique Instance Name in the Properties Inspector. However, there is a better and more efficient way to do this with ActionScript.
When you want your code to repeat the same task many times, you can write a code loop in ActionScript. Basically, this will be a block of code that uses an incremental counter to count the number of instances that are added to the stage. We will then tell Flash to repeat or loop this task until a certain number is reached.
First, we need to make some changes to our SnowScene1.fla Flash movie. Open the file into Flash.
- The first change that we need to make is to remove the three snowflakes from above the stage. Because they are on the Snowflakes layer, we can simply delete this layer. Click on the Snowflakes layer in the Timeline and then click on the Delete Layer icon.
- The second change will be to remove the old ActionScript from the Actions layer and Actions Panel. Click on Frame 1 in the Actions layer and then open the Actions Panel. Highlight and delete all of the code in the Actions Panel.
- Go to the Library Panel and right-click on the mcSnowflake movie clip. Choose Linkage from the pop-up menu. In the Linkage Properties dialog box, click on the checkbox next to Export for ActionScript. You will see that mcSnowflake is already entered as the name for our new class. Keep the default settings and click OK. Flash will ask if you wish to create a definition for our new class. Click OK.
- Now we are ready to add our new ActionScript. Type the following into the Actions Panel.
for (var i:Number = 0; i < 10; i++)
_Snowflake = new mcSnowflake();
_Snowflake.x = i * 50;
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.
Editor's Picks Articles
Top Ten Articles
Content copyright © 2019 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.