Azroc Sitemap

Creating Snow In Flash:

To create snow in flash we will be using the functions duplicateMovieClip(), Math.random(), Math.round() and mx.transitions.Tween(). You might call it motion animation with actionscript. Here is the finished swf:


Open a new document in Flash:

Make it 500 by 400 and set the background to black. Create a new layer and place the following code on the first frame: ( Please note you can download the .fla file to access the code. )

 

function movement(newX:Number, newY:Number, time:Number, mcIn:MovieClip):Void {
// Math.round() generates whole number from decimal
this.targetX = Math.round(newX);
this.targetY = Math.round(newY);
//Easing types easeIn easeOut easeInOut easeNone
//these determine how the tweening movieclip moves
//easeIn - the snowflakes move slowly at first
//then fall faster as approaching the ground

var easeType = mx.transitions.easing.Regular.easeIn;
//the snowflake movie clip
var mc:MovieClip = mcIn;

if (time == undefined) {
time = 7;
}
//mc - the snowflake movieclip
//"_x" - we want to tween in the x direction - left to right "_y" - y direction
//mc._x - the start x coordinate of the movieclip
//this.targetX - where we want the movieclip to end up
//time in this context is actually frames which is the default if the last
//argument to this function is not set to true to tell the fuction to use
//seconds. Please note here because the time is actually 55 frames this
//tween will occur over 55 frames new flakes appear every 5 frames while
//previous flakes continue falling( Tweening )

mc.tween = new mx.transitions.Tween(mc, "_x", easeType, mc._x, this.targetX, time);
mc.tween = new mx.transitions.Tween(mc, "_y", easeType, mc._y, this.targetY, time);
}

 

Remember you can dowload the .fla to access the code and flake_mc. Now drag flake_mc or other movie clip to the stage ( File > Import > Import to stage ). In the properties panel name the instance of flake as flake_mc or modify the code to suit. Create a new layer( called movement on the timeline diagram below ) and in the first frame put the code:

 

//Determines the number of movieclips
var i:Number=0;
//put on frame one of movement layer

 

In the second frame of the duplication layer ( see below ) place the code:


if (i<500){
// duplicate the snowflake and name it 'flakei'
//with unique depth to prevent replacing previous snowflakes

duplicateMovieClip("flake_mc","flake" + i,i);
//initialises the x coordinate of the new movie clip hence it
//appears randomly horizontally from the top of the swf
//between 0 and 500

_root["flake" + i]._x = Math.random() * (500 - 0);
//create another random number and pass it to the
//movement function - movement layer frame 1

var rndX = Math.random() * (500 - 0);
//see movement function frame 1 layer movement
//rndX is the random x coordinate where we want the tween to end up
//400 is the height of the swf or end y value - bottom of swf
//55 is the number of frames over which the tween occurs
//_root["flake" + i] is the movieclip

_root.movement(rndX,400,55,_root["flake" + i]);
// hide the duplicated movieclip because it is
//not passed to the movement function hence is motionless
//or you might say tweenless

_root.flake_mc._visible=false;
}

Finally in order to make it work put the following in frame 5:

 

i++;//this increments the number of movie clips( snow flakes ).
gotoAndPlay(2);//go to frame two and play the timeline hence creating more snowflakes

The time line should look like this:

 

eXTReMe Tracker

Links: