Actionscript 3 Snow Storm Class
It's that time of year again. Winter is just about here and it is time for me to create snow. Last year I did a little demo of creating snow that sticks using Flash 8 and the BitmapData object. I thought that I would take a stab at doing an ActionScript 3 version. So with that said, here is an AS3 snow maker. The movie below obviously requires the Flash 9 player. I am using 1000 flakes in the example below. You can modify that in the FLA. I hope to update this and make it more dynamic in the near future but feel free to play with what is here.
Here is the class:
Here is the class:

Yeah that's cool :D
AS3 Rocks!
Posted by
JBOY |
11:36 AM
that is cool indeed. I didn't know AS3 was out, gotta check it, then.
Posted by
Anonymous |
12:11 PM
looks great.
Posted by
dan |
12:54 PM
Purdy :o)
Campbell A
Posted by
Anonymous |
7:28 PM
Sweet! The killer app of AS3 - a new generation of snow renderers!
Posted by
felix |
11:27 AM
Sprites FTW!
Posted by
Anonymous |
1:34 PM
how did you edit and compile this? i downloaded the fla file and and the class and used the public alpha of flash 9, but the compiler complains when i try to test from flash... any tips on getting started with as3?
Posted by
Anonymous |
7:33 AM
I used the public alpha to compile. The FLA has one frame with the following on it:
function createFlakes():void{
var sFlake:SnowFlake = new SnowFlake();
addChild(sFlake);
}
var numFlakes:int = 1000;
for(var i:int;i<numFlakes;i++){
createFlakes();
}
Posted by
Sarge |
8:09 AM
Wow thats insane
Ive just started using AS3 and Flex 2.0 in the last 3 days and just keep getting amazed at the possibilities.
Great work dude, keep em coming.
Posted by
Anonymous |
2:09 AM
This looks really cool! but i can't get it to work. (note, that i know very little about classes and/or as3)
Could you clarify if the sample works. Or is there something else that must be done. Thank you.
Great job!
Posted by
sam |
3:21 PM
Sam - are you using Flash CS3 or the Flash 9 ActionScript Preview to write your code? Can you send me your FLA? Thanks!
sgtpritchard at gmail.com
Posted by
Sarge |
3:25 PM
I was trying so hard to do something like this!
thx alot for this post.
but i also can't get it to work :S
the class as is there and I'm using your flash file with Flash CS3, AS3 and Flash9 player, only one snowflake is on screen at 0,0
Posted by
Bzouchir |
7:15 PM
It all works now!
I misspelled the file name when saving the class, it didn't match the class name!
for Sam, make sure that you save the class code in "SnowFlake.as"
otherwise the fla won't see it.
Posted by
Bzouchir |
8:47 PM
These comments have been invaluable to me as is this whole site. I thank you for your comment.
Posted by
Annerose |
1:49 PM
Dynamic snow, I love it! Great work.
-mL
http://knowledge.lapasa.net
Posted by
Anonymous |
1:37 PM
Real lovely! The speed and smoothness is so life-like, ActionScript 3 really rocks. Looking at the Class it can easily be extended to accomodate more options such as color, rate and so on. Thanks a million, this class realy really is impressive.
Otuyelu
http://www.oroede.org
Posted by
Olumide |
4:11 AM
Very new to flash. Trying to run befor I can walk. How do I put the class file to the Flash file to get it to work. Any help Thanks.
Posted by
Anonymous |
1:42 PM
Sorry for leaving my last comment twice. and with a bit of head scratching and a lot of googleing I'v sused it. It's all to do with the class path init?
Thanks and keep up the good work.
Posted by
Anonymous |
2:34 PM
Awesome class! I have built a class called SnowStorm around it that both extends it and black boxes it. Simple instantiation, start/stop, and black-box access to properties.
I have had to make a few minor mods to the SnowFlake class to allow for this, but really didn't change it much. Mods and new features include:
-Snowflake draws itself as a graphic instead of using a library movieclip. This makes it an independant class that does not need to load any library items from an FLA.
-Extended properties like size and color of snowflake, and speed variables so that they can be easily accessed as properties of the class (and all properties simply default so you do not have to set any).
-Added automatic stage-size detection, unless you override that by setting the viewWidth and viewHeight properties yourself.
-Added start/stop features to the snowfall, which can be passed a delay (in seconds) that it takes for the storm to get started. For example you could call begin(30) and the snowfall would increase slowly over 30 seconds until its going full on.
The code in the SnowStorm class is commented and I have also included an FLA to test it out with immediately.
Linking temporarily from my site because I don't see a way to include code (or files) in the comments!
========================
SnowStorm.as
SnowFlake.as
SnowStormDemo.fla
SnowStormDemo.swf
========================
Posted by
Egg_Zap |
9:57 PM
Shouldn't you use Sprites instead of MovieClips for your SnowFlakes, because it would save a lot of memory since there are 1000 snowflakes.
Posted by
zedia.net |
2:07 PM
If I add a public method to SnowFlake class and call it from mainline script, I get null reference error. How can I overcome this? Please help.
Posted by
Anonymous |
7:16 PM
I guess I'd need you to be more specific. What public method are you adding and what are you trying to return?
Posted by
Ben |
8:45 PM
I'm a new user of action script, coul you tell me where have I to put the code?
Posted by
jdm |
12:57 PM
jdm, take a look at the FLA here: http://www.pixelfumes.com/blog/nov06/snowFlake.fla and see if that helps.
-Ben
Posted by
Ben |
1:39 PM
amazing!
Posted by
alx |
12:17 PM
Totally awesome effect! Using it a lot!
Posted by
Rob Zanin |
1:33 PM
This is pretty sweet.
However, I am pretty new to as3, and really dont know how to get this to work..
Any help would be appreciated.
Posted by
WoCo |
8:33 AM
This is great! I added a few functions to your class that stop snow from falling. Thought someone might benefit from it:
// Call this function to stop the flakes from falling.
// You will need to specify which everyFrameEnd method to use (see below)
public function stopFlakes():void {
this.removeEventListener(Event.ENTER_FRAME, everyFrame);
this.addEventListener(Event.ENTER_FRAME, everyFrameEnd2);
}
//This function stops new flakes from falling, while the current flakes eventually dissapear off the bottom of the screen.
private function everyFrameEnd(e:Event):void{
this.y += speed;
this.x += drift;
if(this.y > viewHeight || this.x < 0 || this.x > viewWidth){
this.alpha = 0;
this.removeEventListener(Event.ENTER_FRAME, everyFrameEnd);
}
}
//This function fades all the flakes out (quicker than using above)
private function everyFrameEnd2(e:Event):void{
this.y += speed;
this.x += drift;
this.alpha -= .025;
if(this.y > viewHeight || this.x < 0 || this.x > viewWidth) {
this.removeEventListener(Event.ENTER_FRAME, everyFrameEnd2);
this.alpha = 0;
}
}
Posted by
David |
2:48 PM
I absolutely love this! Great example, and very cool you share your source...nothing is more frustrations that those who don't share. hah!
-Justin
http://www.noisydesign.com
Posted by
Justin |
10:52 PM