Tutorials Books Videos Forums

-- online Change the theme! Search!
Rambo ftw!

Customize Theme


Color

Background


Done

Falling Snow for Flash CS4/AS3

by kirupa   | filed under Flash and ActionScript

This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.

Every year during Christmas time, unsurprisingly, the falling snow tutorial is one of the most popular tutorials on the site. In case you don't know what exactly it is, click on the Let It Snow banner in the below movie:

Once you have clicked on the Let It Snow banner, you'll see a fair number of snow-like circles oscillating as they fall to the ground....just like real snow!

This effect is something that has been covered on the site extensively over the years, but this one is newer and shinier. It is newer and shinier because, in this tutorial, you will learn how to implement the snow effect using ActionScript 3 in Flash CS4 instead of something older.

So what are you waiting for? Let's get started!

Setting up the Stage

Let's start at the very beginning and work our way up:

  1. First, create a new animation in Flash CS4 whose project type is Flash File (ActionScript 3.0). Once your project has been created, from the Properties panel, click the button next to the Size text called Edit. The Document Properties dialog will appear.
  2. From the Document Properties dialog, set the animation's width and height to 450 pixels by 300 pixels respectively:

[ set your animation's width/height to 450 by 400 ]

  1. From this same dialog, set the frame rate to 30 and the background color to black:

[ since you are already here, set the frame rate to 30 ]

The animation size, background color, and frame rate are the only things that you will need to change from the Document Properties dialog. Once you have made these changes, go ahead and click OK to commit the changes and to close the dialog.

Your stage will now look as follows:

[ wow, this is a work of art ]

With your stage setup, let's move on to the bigger piece that is creating the SnowFlake movie clip.

Creating the SnowFlake Movie Clip

Each circle that you see falling down is a movie clip with a class file associated with it. If the preceding sentence did not make a lot of sense to you, I strongly suggest you read my earlier Classes and Movie Clips article before continuing on.

What we are going to do now is draw a circle, make it into a movie clip, and edit the movie clip's properties to require a class file be used for defining its functionality.

Let's get started:

  1. Using the Circle tool, draw a small, white circle anywhere on your stage:

[ this circle will be your snowflake ]

  1. Once you have drawn the circle, select it (if you somehow deselected it), and press F8 (Modify | Convert to Symbol) to launch the Convert to Symbol dialog. Once this dialog appears, make sure Type is set to Movie Clip, and in the Name field, enter Snowflake:

[ conver the circle into a movie clip symbol ]

  1. From this same dialog, now hit the Advanced button to display more customizations you can make to the movie clip you will eventually be creating. There should be an area marked Linkage that contains two checkboxes. Click on the first checkbox that says Export for ActionScript:

[ we want to have a class file assocated with this circle ]

Once you have checked that box, a few more fields will be autocompleted for you. Notice that the Class name is listed as Snowflake. That will come in handy in a little bit.

  1. Press OK to create your Snowflake movie clip and to close this dialog. You may see a warning dialog stating that the class file cannot be found, but don't worry about that. Click OK to acknowledge and close the dialog.

    If you look in your Library panel, you will see your Snowflake movie clip displayed:

[ your library will now display your movie clip ]

  1. You are almost done with this part of the tutorial. Go to File | Save and save this your Flash document (FLA) into a directory. Saving the file is important for you will revisit the location this FLA file is saved in shortly.

Ok, this seems like a good place to take a small break. In the next section, we'll create the class file that links the movie clip you created with the code that will power it.


In the previous section, you setup your stage and created the Snowflake movie clip. In this page, let's create the class file the movie clip will refer to.

Creating the Snowflake Class File

Ok, let's create this class file once and get it over with:

  1. From inside Flash CS4, go to File | New to display the New Document dialog.
  2. Once the New Document dialog has been launched, select ActionScript File from the list of document types listed:

[ create a new ActionScript File ]

  1. With the ActionScript File type selected, click OK to create the new file. The New Document window will disappear and you will see the class file created and opened for you:

[ the blank code editor represents your class file ]

  1. This ActionScript file doesn't actually live on your disk yet. It is just something that you see in Flash, so let's make it permanent by saving it. Go to File | Save to launch the Save As dialog. Browse to the directory your current Flash (FLA) file is in and give your file the name Snowflake:

[ gives this class file the name Snowflake - with the capital 'S' ]

Note, it is extremely important that you save your Snowflake ActionScript file in the same directory your Flash source file is in. For example, here is my directory where the FLA file and the AS file are stored:

[ the FLA and the AS file are in the same folder - such a happy family ]

  1. Go back into Flash and make sure your Snowflake AS file is currently open for editing. Right now, it is just a blank document with nothing in it. Let's go ahead and add the code that makes it all work.

    Copy and paste the following code into your Snowflake AS file:
package
{
  import flash.display.*;
  import flash.events.*;
  public class Snowflake extends MovieClip
  {
  private var xPos:Number = 0;
  private var yPos:Number = 0;
  private var xSpeed:Number = 0;
  private var ySpeed:Number = 0;
  private var radius:Number = 0;
  private var scale:Number = 0;
  private var alphaValue:Number = 0;
  private var maxHeight:Number = 0;
  private var maxWidth:Number = 0;
  public function Snowflake()
  {
  SetInitialProperties();
  }
  public function SetInitialProperties()
  {
  //Setting the various parameters that need tweaking
  xSpeed = .05 + Math.random()*.1;
  ySpeed = .1 + Math.random()*3;
  radius = .1 + Math.random()*2;
  scale = .01 + Math.random();
  alphaValue = .1 + Math.random();
  var stageObject:Stage = this.stage as Stage;
  maxWidth = stageObject.stageWidth;
  maxHeight = stageObject.stageHeight;
  this.x = Math.random()*maxWidth;
  this.y = Math.random()*maxHeight;
  xPos = this.x;
  yPos = this.y;
  this.scaleX = this.scaleY = scale;
  this.alpha = alphaValue;
  this.addEventListener(Event.ENTER_FRAME, MoveSnowFlake);
  }
  function MoveSnowFlake(e:Event)
  {
  xPos += xSpeed;
  yPos += ySpeed;
  this.x += radius*Math.cos(xPos);
  this.y += ySpeed;
  if (this.y - this.height > maxHeight)
  {
  this.y = -10 - this.height;
  this.x = Math.random()*maxWidth;
  }
  }
  }
}
  1. If you save your class file and test your movie out, you'll see your lone little snowflake slowly oscillating its way down.

Wow, you covered quite a bit of ground this page. All of this has basically allowed you to have one snowflake fall. Don't worry, there is more, so let's look at how to turn your single snowflake into a mild winder storm in the next section.


In the previous section, you finished creating your Snowflake movie clip and got a single snowflake working. For the most part, you don't want to have just a single snowflake falling. You will want more, and there are two ways you can have more snowflakes fall.

One way is by manually creating Snowflake movie clips, and the other way is by programmatically creating snowflakes. Let's look at how to accomplish both of them...starting with the manual approach on this page.

Creating Snowflakes Manually

To create snowflakes manually, open your FLA file where you will see your single, lonely snowflake movie clip:

[ a singleton ]

Select your movie clip and press Ctrl + C to copy it. Once you have copied the movie clip, press Ctrl + V and paste it. You will now see another movie clip:

[ two of a kind ]

Two is still not enough. So keep copying, pasting, and repositioning movie clips until you have something that is worth writing home about:

[ many two of a kinds ]

Once you have enough movie clips, press Ctrl + Enter to preview your application. Notice that now you see many snowflakes doing what they do best - falling:

[ what you see as your output ]

Right now, you are probably very content. There may be one thing that you may not like about the final output. Notice how the snowflakes look on your stage and notice how the snowflakes look in the final version:

Programmatically, the snowflake you drew is randomly scaled and given a dash of transparency to make each snowflake unique. If you want precise control over the look and feel of each of your snowflakes, you don't want this. Fortunately, overriding this functionality requires commenting out (or removing) just a few lines of code.

To prevent the random sizing and alpha'ing of your snowflakes, in your Snowflake.as file, comment out or remove the highlighted lines found below:

package
{
  import flash.display.*;
  import flash.events.*;
  public class Snowflake extends MovieClip
  {
  private var xPos:Number = 0;
  private var yPos:Number = 0;
  private var xSpeed:Number = 0;
  private var ySpeed:Number = 0;
  private var radius:Number = 0;
  private var scale:Number = 0;
  private var alphaValue:Number = 0;
  private var maxHeight:Number = 0;
  private var maxWidth:Number = 0;
  public function Snowflake()
  {
  SetInitialProperties();
  }
  public function SetInitialProperties()
  {
  //Setting the various parameters that need tweaking
  xSpeed = .05 + Math.random()*.1;
  ySpeed = .1 + Math.random()*3;
  radius = .1 + Math.random()*2;
  scale = .01 + Math.random();
  alphaValue = .1 + Math.random();
  var stageObject:Stage = this.stage as Stage;
  maxWidth = stageObject.stageWidth;
  maxHeight = stageObject.stageHeight;
  this.x = Math.random()*maxWidth;
  this.y = Math.random()*maxHeight;
  xPos = this.x;
  yPos = this.y;
  this.scaleX = this.scaleY = scale;
  this.alpha = alphaValue;
  this.addEventListener(Event.ENTER_FRAME, MoveSnowFlake);
  }
  function MoveSnowFlake(e:Event)
  {
  xPos += xSpeed;
  yPos += ySpeed;
  this.x += radius*Math.cos(xPos);
  this.y += ySpeed;
  if (this.y - this.height > maxHeight)
  {
  this.y = -10 - this.height;
  this.x = Math.random()*maxWidth;
  }
  }
  }
}

Once you comment out or remove those lines of code,  your final animation will now feature snowflakes that look exactly how you defined them. For just plain circles, that may not be exactly what you want, but as you will see later, changing from a circle to some other shape is fairly easy.

Anyway, this wraps up the manual approach where each snowflake was manually copied and pasted. The other approach is to programmatically generate the snowflakes, and we'll look into doing just that in the next section.


In the previous section, you learned how to create a snow flurry by manually duplicating movie clips. In this page, you will create a similar effect. The difference is that you will do that programmatically instead of manually copying and pasting.

Creating Snowflakes Programmtically

To create snowflakes automatically, first clear your stage of any snowflakes. That means that your single solitary snowflake has to be removed as well.

[ yes, I did reuse the image from the previous section ]

What you will have now is nothing more than a black background with nothing in it. As strange as that sounds, that is exactly what we want.

Now, go over to your timeline, right click on the first frame, and select Actions (from the menu that appears). The Actions window will now be visible. Copy and paste the following code:

function makeItSnow()
{
  for (var i:int = 0; i < 100; i++)
  {
  var newSnowFlake:Snowflake = new Snowflake();
  this.addChild(newSnowFlake);
  newSnowFlake.SetInitialProperties();
  }
}
makeItSnow();

Don't test your application just yet, for there are some changes that you need to make to your Snowflake.as file. Open your Snowflake class file and delete or comment out the highlighted line:

package
{
  import flash.display.*;
  import flash.events.*;
  public class Snowflake extends MovieClip
  {
  private var xPos:Number = 0;
  private var yPos:Number = 0;
  private var xSpeed:Number = 0;
  private var ySpeed:Number = 0;
  private var radius:Number = 0;
  private var scale:Number = 0;
  private var alphaValue:Number = 0;
  private var maxHeight:Number = 0;
  private var maxWidth:Number = 0;
  public function Snowflake()
  {
  SetInitialProperties();
  }
  public function SetInitialProperties()
  {
  //Setting the various parameters that need tweaking
  xSpeed = .05 + Math.random()*.1;
  ySpeed = .1 + Math.random()*3;
  radius = .1 + Math.random()*2;
  scale = .01 + Math.random();
  alphaValue = .1 + Math.random();
  var stageObject:Stage = this.stage as Stage;
  maxWidth = stageObject.stageWidth;
  maxHeight = stageObject.stageHeight;
  this.x = Math.random()*maxWidth;
  this.y = Math.random()*maxHeight;
  xPos = this.x;
  yPos = this.y;
  this.scaleX = this.scaleY = scale;
  this.alpha = alphaValue;
  this.addEventListener(Event.ENTER_FRAME, MoveSnowFlake);
  }
  function MoveSnowFlake(e:Event)
  {
  xPos += xSpeed;
  yPos += ySpeed;
  this.x += radius*Math.cos(xPos);
  this.y += ySpeed;
  if (this.y - this.height > maxHeight)
  {
  this.y = -10 - this.height;
  this.x = Math.random()*maxWidth;
  }
  }
  }
}

Once you have commented out that line, test your application by pressing Ctrl + Enter or by going to Control | Test Movie. You will see 100 snowflakes animating and falling to the ground. Best of all, you didn't actually have to manually specify each of the 100 movie clips. They were automatically placed on the stage for you.

Code Explained

In this and the previous few pages, you've been exposed to a lot of code that I never explained fully how it works. Fortunately, the code presented here is not that much different from the code that you have seen in some earlier tutorials. Therefore, instead of explainining the code, I am going to point you to other tutorials that will help you out instead: Colorful Explosion, Classes and MovieClips, Trigonometric Animations. I am sure after reading those three tutorials - especially Colorful Explosion - how to programmatically animate movie clips will be made very clear.

Anyway, despite me weaseling out of explaining how the code works, you aren't done yet. In the next section, let's look at how to make some common changes and modifications to this falling snow effect.


In the previous sections, you started from scratch and created a falling snow effect. Outside of two variations in how your snowflakes get geneated, you really haven't customized your snow effect in any great form or fashion. In this page, let's look at a few of those customizations and how to go about doing them.

Changing the Snowflake

It should be no surprise by now that the snowflake you see is just a filled-in circle. A real snowflake looks quite a different than that, so if you wanted to change the look of your snowflake, how would you do it? You would do that by editing the look of your Snowflake movie clip itself.

In Flash, make sure your Library panel is visible. Inside your Library panel, you will only see one thing - your Snowflake movie clip:

[ your Snowflake movie clip can be accessed via the Library ]

Right click on the Snowflake movie clip name and select Edit. Your stage will now be showing the contents of your Snowflake movie clip which, as I mentioned earlier, is just a circle:

[ you are now editing the Snowflake file ]

Replace this circle with whatever element that you want. Just for kicks, I decided to replace the circle with that of the Santa Claus smiley face you have on the forums. The end result is as follows:

[ even I think this looks ridiculous ]

Of course, I'm hoping you'll find something a bit more tasteful to replace the white circle with haha.

Adding Rotation

A snowflake doesn't fall without some slight rotation. If you do decide to go with a non-circular snowflake, the rotation will add a really nice touch of realism to this effect. This does require some changes to be made in code. In your Snowflake class file, find the MoveSnowFlake function and add the line of code that I've highlighted below into the same location:

function MoveSnowFlake(e:Event)
{
  xPos += xSpeed;
  yPos += ySpeed;
  this.x += radius*Math.cos(xPos);
  this.y += ySpeed;
  if (this.y - this.height > maxHeight)
  {
  this.y = -10 - this.height;
  this.x = Math.random()*maxWidth;
  }
  this.rotation += xSpeed;
}

Once you have added this line of code, you will find that your snowflake will slowly rotate as it is heading towards the ground.

Adding More Snowflakes

The final customization I will explain is how to add more snowflakes. For the manual case, you know how to do that easily - you just copy and paste in a few more snowflakes. For the automatic case, you will have to change a number that indicates how many snowflakes you want.

You copied and pasted some code that looks like the following:

function makeItSnow()
{
  for (var i:int = 0; i < 100; i++)
  {
  var newSnowFlake:Snowflake = new Snowflake();
  this.addChild(newSnowFlake);
  newSnowFlake.SetInitialProperties();
  }
}
makeItSnow();

In that code, change the highlighted numberf from 100 to whatever number you want. This number specifies the number of snowflakes you will have in your snow shower. Do note that having a large number of snowflakes will cause high CPU usage on even the fastest of machines.


Conclusion

Well, that's all there is to creating your own falling snow effect. As you can see, this is one of those effects that is more code heavy than design heavy. All you did was draw a circle and convert it into a movie clip. All of the remaining magic was performed using the ActionScript code that you copied and pasted. Hopefully the tutorials I referred to earlier help decipher how the code actually work to do what you see.

In case you are curious to see how my version of the falling snow effect looks like, the following Zip file contains both the manual as well as automatic approaches for generating a snow storm:


Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--