Blog

a great conversation starts with a great topic

This is a public Blog  publicRSS

Entry

    • Flash Snow Flake Fun in AS3
      Entry posted 12/22/08 by Dan.Smith
      805 Views, 0 Comments
      Title:
      Flash Snow Flake Fun in AS3
      Entry:

      Here’s a little seasonal fun using Flash AS3 to create some falling snow flakes for your holiday enjoyment.

      First you need to create the falling flake which will be dynamically duplicated using actionScript 3.  Here’s a little snap of my flake:

      The flake is tweened and follows the guide downward.  I also added some rotation to the tween.

      Next, you’ll need the duplication script which does all the work:

      //number of flakes
      var myFlakes = 200;
      //size of flakes
      var mySize = 2.2;

      function dupFlakes(target:DisplayObject, autoAdd:Boolean = false):DisplayObject {

      var targetClass:Class = Object(target).constructor;
      var duplicate:DisplayObject = new targetClass();

      duplicate.transform = target.transform;
      duplicate.filters = target.filters;
      duplicate.cacheAsBitmap = target.cacheAsBitmap;
      duplicate.opaqueBackground = target.opaqueBackground;

      if (autoAdd && target.parent) {
      target.parent.addChild(duplicate);
      }
      return duplicate;
      }

      for (var i=0; i<=myFlakes; i++) {
      var r = Math.round(Math.random()*100);
      var myCopiedSprite:MovieClip = dupFlakes(flake_mc,true) as MovieClip;
      var myNum = Math.round(Math.random()*550);
      myCopiedSprite.x = Math.round(Math.random()*1000);
      myNum = Math.round(Math.random()*mySize);
      myCopiedSprite.scaleX += myNum;
      myCopiedSprite.scaleY += myNum;
      myCopiedSprite.gotoAndPlay(r);
      }

      The code duplicates our desired number of flakes and sets the size.  Since we don’t want all the flakes to fall simutaniously, we need to tell them to start at different times. Hence, the gotoAndPlay at a random point in the flakes path.

      Here’s a snap of the finished product:

      Mess around with it and happy holidays!

      the files:
      fallingSnowAS3.fla

      fallingSnow.swf

      Keywords:
      Flash, Snow, duplicateClip