Printer Friendly Version

BellaOnline's Flash and Animation Editor

ActionScript For Loop Code and Multiple Instances of Movie Clips

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 →

Flash and Animation Site @ BellaOnline
View This Article in Regular Layout

Content copyright © 2013 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.

| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2016 Minerva WebWorks LLC. All rights reserved.

BellaOnline Editor