3D Snow in Flash player 10

Well, I have been really busy lately working on various projects (to be posted here soon hopefully) and as this is my first post in a while I thought why not keep in tune with the current season and make it a festive one.

As the new Flash player 10 enables the use of 3D space I thought it fitting to create a snowfall in 3D to make it feel as if you really are out in the bleak cold of winter on a cold christmas morning…….This one is created exclusivley for FP10 so you’ll need that installed in your browser to view this and the source files are Flash CS4 / Flex only….?!

The script makes use of the Z property in the new flash API and creates a good feeling of depth. As you can see from the script, the Vector3D class is initiated to add some good X, Y & Z figures to the flakes and gives them some good random motion.

< view plain text >
  1. private function init(e:Event):void {          
  2.             startX = (-stage.stageWidth / 2) + (Math.random() * stage.stageWidth);
  3.             this.scaleX = Math.random() * .9;
  4.             this.scaleY = this.scaleX;
  5.             this.x = startX;
  6.             this.y = -(Math.random() * (stage.stageHeight + 200));
  7.             this.z = -500 + (Math.random() * 800);
  8.             vector = new Vector3D(0, 0, 0);
  9.             vel = new Vector3D((Math.random()*1.5), -3, (Math.random()*1.5), (Math.random()*10));
  11.         }

The flake class creates the flake from the movieclip linked to it in the Flash CS4 library and gives it some random size, position as well as movement. This is then added in the main document class during the “FOR” loop along with 299 of its brothers and sisters. An ENTER_FRAME event listener is added after this and calls the updateFlake function of each individual snow flake which dictates their movements. As this is called each frame the movements of all 300 flakes are created each frame.

< view plain text >
  1. public function updateFlake():void{
  2.             this.x -= vel.x;
  3.             this.y -= vel.y;
  4.             this.z -= vel.z;
  5.             var dx:Number = vector.z - this.z;
  6.             var dy:Number = vector.y - this.y;
  7.             var b:Number = (Math.sqrt(dx*dx + dy*dy) / 100) + 7;
  8.             if(b < 0){
  9.                 b = Math.abs(b);
  10.             }
  11.             blur = new BlurFilter(b, b, 3);
  12.             this.filters = [blur];
  13.             if(this.y > (stage.stageHeight + 100)){
  14.                 this.y = -100;
  15.                 this.x = startX;
  16.                 this.z = -500 + (Math.random() * 800);
  17.             }
  18.         }

Once they reach a certain point on the stage they are immediately moved back to their starting point, thus creating a continual loop of soft white fluffy floaty snow……..:)

As usual source file are provided here.

This entry was posted in Actionscript 3.0, AS3 3D, Flash Player 10 and tagged , , , , . Bookmark the permalink.

3 Responses to 3D Snow in Flash player 10

  1. Bruno Santos says:

    Hello, the download link is broken.

    Congratulations by the article.


  2. UltraVisual says:

    Hello back…..works fine for me……try again. Thanks

  3. Mildoo says:

    Great tutorial many thanks..

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>