Creating Falling Snow using AS3.0

December 17th, 2007     |     View Comments

I’ve received a lot of requests to update my earlier Falling Snow animation. That kind of makes sense since this is the time of year when many of you can get away with adding an animation that simulates falling snow! In this post, I will describe how to create the falling snow effect in Flash CS3 using ActionScript 3.0.

I have posted a screenshot of it in action below:

fallingSnow

To see this in action, feel free to download and extract the source files that demonstrate everything shown in this post. The code for this is very straightforward, and I have pasted my SnowFlake.as file below:

package {
import flash.display.*;
import flash.events.*;
 
public class SnowFlake extends MovieClip {
 
var radians = 0;
var speed = 0;
var radius = 5;
var stageHeight;
 
public function SnowFlake(h:Number)
{
speed = .01+.5*Math.random();
radius = .1+2*Math.random();
 
stageHeight = h;
 
this.addEventListener(Event.ENTER_FRAME, Snowing);
}
 
function Snowing(e:Event)
{
radians += speed;
 
this.x += Math.round(Math.cos(radians));
this.y += 2;
 
if (this.y > stageHeight)
{
this.y = -20;
}
}
}
}

I will be adding a tutorial later in the week that goes into what many of these lines do in greater detail, but as you can see, a lot of the code is pretty straightforward. There is a steep learning curve from AS 2.0 to AS 3.0 if you don’t have any prior experience with an OOP language, but it simply requires associating what you already know to the new syntax and rules.

Beyond the code in SnowFlake.as, there is some code in the first frame of the movie where I populate the stage with snowflakes, and that code is:

function DisplaySnow()
{
for (var i:int = 0; i < 40; i++)
{
var snowFlake:SnowFlake = new SnowFlake(250);
this.addChild(snowFlake);
 
snowFlake.x = Math.random()*350;
snowFlake.y = Math.random()*250;
snowFlake.alpha = .2+Math.random()*.5;
 
var scale:Number = .3+Math.random();
snowFlake.scaleX = snowFlake.scaleY = scale;
}
}
DisplaySnow();

For details on how this section of code works, you should look at my earlier Displaying Library Content in AS3.0 article I wrote. Anyway, I hope this helps you learn a little bit more about not only how to simulate falling snow but more on how you can do this in ActionScript 3.0.

Cheers!
Kirupa :)

View Comments to “Creating Falling Snow using AS3.0”

  1. Krilnon Says:

    Falling Snow 4.0!

    You might throw people off by using C# function naming conventions (upper case first letter). =P

    It _might_ be a good idea to mention (in a note) how one could make the snow fall upside down (and become bubbles). That way, you would avoid having 30 forum threads asking for it.

  2. chandan Says:

    Hi i am chitranjan singh i wana hitTest of th movi clicp how can i do the text to movi clip and hitTest than provide score.pls give the solution
    MovieClips.hitTest();

    thankx………

  3. Chakkaradeep Says:

    Now lets wait until Kirupa comes out with a WPF version for Microsoft Developers like me :)

    Comeon Kirupa ;)

  4. kirupa Says:

    Chakka – CompositionTarget.Rendering is your friend :)

    I’ll post one up shortly.

  5. kirupaBlog - If it isn’t broken, take it apart and fix it! » Blog Archive » Creating Falling Snow in WPF/C# Says:

    [...] In an earlier post, I described how to create a falling snow effect using ActionScript 3.0 (AS3). Because the way you write and structure your code in AS3 is similar to that of other mainstream languages like C# or Java, porting your code from one language to another becomes much easier. [...]

  6. Angel Romero Says:

    Nice bit of code. I can see this script been used for multiple type of particles.

  7. andy Says:

    thanks for the update =)

    i’ve just run the source file and i’m not quite happy with the easing effect, seems alittle robotic =P i remeber the as2 version looking more natural.

    any chance you could tweak it?

  8. kirupa Says:

    Andy – this is an almost identical port of the AS2 version. There never was any easing applied to the snow, but I may look into it for the future :P

  9. ragz Says:

    WOW nice tutorial was looking for this. I am new to actionscript and this really helped me for my project. So created a tinybin link & sharing with my frnds. Thanks a lot
    Here is the tinybin link of this post
    http://tinybin.com/falling-snow

  10. rpeelTNT Says:

    Hey, just found your tut for “Cerate a Photo Gallery”, anyway you could help me adapt it to allow for clickable thumbs? I can’t get it to access the array while keeping the fadein/out look.
    thanks

  11. Строитель Says:

    Я так и думал, автор спасибо)

  12. link wheel Says:

    Wonderful information here. This fascinating put up made me smile. Perhaps for those who throw in a couple of footage it would make the entire thing more interesting. Anyway, in my language, there aren’t much good source like this.

  13. sim so dep Says:

    Thank you for the wise critique. Me & my neighbour have been getting ready to do a little analysis about that. We received an excellent guide on that matter from our local library and most books where not as influensive as your information. I am very glad to see such information which I used to be searching for a protracted time.This made very glad! Anyway, in my language, there aren’t a lot good source like this.

  14. south beach diet recipes Says:

    Glorious data here. This interesting post made me smile. Possibly should you throw in a few pictures it would make the entire thing extra interesting. Anyway, in my language, there usually are not much good supply like this.

  15. Mariquita Says:

    You really make it seem so easy with your presentation but I find this matter to be actually something which I think I would never understand. It seems too complex and very broad for me. I’m looking forward for your next post, I’ll try to get the hang of it!

  16. rss feed Says:

    Unbelievable rss feed submit! This could rss feed support a number of individuals rss feed find out about this matter. Do you need to incorporate video clips along with these? It rss feed could undoubtedly help out. rss feed Your purpose was spot on and owing to you; I most likely won’t have to explain all the pieces to my pals. I rss feed can merely direct them here. Anyway, in my language, there will not be a lot good source like this.

  17. Digital Media Sales Says:

     Moving out has its drawbacks as well. It will be kind of a bummer to not be able to walk around the back of my house in my poncho and slippers to my neighbor’s house and ask what they are having for breakfast or lunch or dinner and ask if I can stay.

  18. Cellulite Says:

    good morning! I just wish to give a huge thumbs up for the very good data?s you haven here on this post. I shall be coming back to your weblog for extra soon. This actual answered my downside?s, thank?s for this information! sincerely Jasmin

  19. Ardith Walling Says:

    Hi! :) Is it Okay if I ask something kinda off subject? I’m trying to view this web page on my new iPad nevertheless it will not exhibit up correctly, do you’ve any alternatives? Must I try and locate an update for my software program or anything? Thanks upfront! Jennine x :)

  20. Reina Loughney Says:

    I totally agree, but the points could easily be stated in a clearer fashion, thats all I was saying. No prob here bro, Im not that uptight about it.

  21. cellulose extraction thimbles Says:

    I have often debated the points you made in your article with others. You did however, make some points I missed and I appreciate the fresh perspective on this subject.

Leave a Reply

blog comments powered by Disqus