ActionScript For Loop Code

ActionScript For Loop Code
In our first FlashR particle system snow scene tutorial, we learned how to use a modular function and function call written in ActionScript to animate more than one instance of the same movie clip. This was a very simple way to perform this common task in Flash. In this second tutorial, we will take a look at more programming terminology. We will discuss code loops and how they can be used to perform a task as many times as you wish.

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.

  1. 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.

  2. 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.

Now we are ready to build our new version of the snow scene. Because we removed the three snowflakes from above the stage, we no longer have any instances of our mcSnowflake movie clip on the stage. Therefore, we need to find a new way to set up the movie clip to be controlled by ActionScript. We will use Linkage to specify that the mcSnowflake symbol be exported for ActionScript at runtime. This is just a fancy way to tell Flash that this movie clip which is stored in the Library needs to be used in our movie.

  1. 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.

  2. Now we are ready to add our new ActionScript. Type the following into the Actions Panel.

    var _Snowflake:mcSnowflake;

    for (var i:Number = 0; i < 10; i++)
    _Snowflake = new mcSnowflake();
    _Snowflake.x = i * 50;

Let's take a look at each line of code.

Next →

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.