logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
European Travel
Action Movies
Bible Basics
Houseplants
Romance Movies
Creativity
Family Travel


dailyclick
All times in EST

Clairvoyance: 08:00 PM

Full Schedule
g
g Flash and Animation Site

BellaOnline's Flash and Animation Editor

g

ActionScript For Loop Code and Multiple Instances of Movie Clips - 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.

i++
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();
addChild(_Snowflake);
_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.

addChild(_Snowflake);
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





Add ActionScript+For+Loop+Code+and+Multiple+Instances+of+Movie+Clips+%2D+The+Code to Twitter Add ActionScript+For+Loop+Code+and+Multiple+Instances+of+Movie+Clips+%2D+The+Code to Facebook Add ActionScript+For+Loop+Code+and+Multiple+Instances+of+Movie+Clips+%2D+The+Code to MySpace Add ActionScript+For+Loop+Code+and+Multiple+Instances+of+Movie+Clips+%2D+The+Code to Del.icio.us Digg ActionScript+For+Loop+Code+and+Multiple+Instances+of+Movie+Clips+%2D+The+Code Add ActionScript+For+Loop+Code+and+Multiple+Instances+of+Movie+Clips+%2D+The+Code to Yahoo My Web Add ActionScript+For+Loop+Code+and+Multiple+Instances+of+Movie+Clips+%2D+The+Code to Google Bookmarks Add ActionScript+For+Loop+Code+and+Multiple+Instances+of+Movie+Clips+%2D+The+Code to Stumbleupon Add ActionScript+For+Loop+Code+and+Multiple+Instances+of+Movie+Clips+%2D+The+Code to Reddit



 



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


For FREE email updates, subscribe to the Flash and Animation Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


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

g


g features
The Motion Toolbar Tools

Apple Motion 5 - The Toolbar

Apple Motion - HUD

Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


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


BellaOnline Editor