You are viewing the archives of the original Pixelfumes blog. This blog is no longer maintained. All of the content available on this site is available on the new Pixelfumes blog in addition to all of our new posts! Visit: http://blog.pixelfumes.com.

« Home | Awesome Interactive Flash Displays 3D Animations o... » | Portions of the Flash AVM2 Released as Open Source... » | Multiple Instances of an AS2 Class Sharing Variabl... » | Foster's Home For Imaginary Friends - Cheese Pumpk... » | Pixelfumes.com Redesigned » | Flash 8 Active Blur - Blur Behind a Transparent Ob... » | 10th Anniversary of Flash Animation by Nectarine » | Fullscreen Webcam with Live Reflection Using New F... » | Flash onSoundLoad Event Bug - Not Firing » | Fullscreen Flash 9 - Look Ma, No Chrome! » 

Monday, November 27, 2006 

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:




Yeah that's cool :D

AS3 Rocks!

that is cool indeed. I didn't know AS3 was out, gotta check it, then.

looks great.

Purdy :o)

Campbell A

Sweet! The killer app of AS3 - a new generation of snow renderers!

Sprites FTW!

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?

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();
}

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.

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!

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

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

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.

These comments have been invaluable to me as is this whole site. I thank you for your comment.

Dynamic snow, I love it! Great work.

-mL
http://knowledge.lapasa.net

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

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.

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.

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
========================

Shouldn't you use Sprites instead of MovieClips for your SnowFlakes, because it would save a lot of memory since there are 1000 snowflakes.

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.

I guess I'd need you to be more specific. What public method are you adding and what are you trying to return?

I'm a new user of action script, coul you tell me where have I to put the code?

jdm, take a look at the FLA here: http://www.pixelfumes.com/blog/nov06/snowFlake.fla and see if that helps.
-Ben

amazing!

Totally awesome effect! Using it a lot!

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.

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;
}
}

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

Post a Comment