In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Explosion Smoke

Get Adobe Flash player
by romatica 25 Sep 2012
/**
 * Copyright romatica ( http://wonderfl.net/user/romatica )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/4wGX
 */

package
{
    import a24.tween.Ease24;
    import a24.tween.Tween24;

    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.BitmapDataChannel;
    import flash.display.BlendMode;
    import flash.display.GradientType;
    import flash.display.Graphics;
    import flash.display.Shape;
    import flash.display.SpreadMethod;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import flash.utils.Dictionary;

    [SWF(backgroundColor="#000000", frameRate="60", width="465", height="465")]
    /**
     * 
     */
    public class Wonderfl_Smoke extends Sprite
    {
        public var smokeAlpha : Number = 1;
        
        private static const RECT : Rectangle = new Rectangle(0, 0, 256, 256);
        private static const SPEED : Number = -1.5;
        private static const SEED : Number =  Math.random();
        private static const P : Point =  new Point();
        private var gColor : Array = [0x000000, 0x000000];
        private var gAlpha : Array = [0, 255];
        private var noizBmd : BitmapData;
        private var bm : Bitmap;
        private var bmd : BitmapData;
        private var tf : TextField;
        private var offsets : Array;
        private var gDic : Dictionary;

        public function Wonderfl_Smoke()
        {
            if(stage) _initialize(null); 
            else  addEventListener(Event.ADDED_TO_STAGE, _initialize);
        }

        private function _initialize(event : Event) : void
        {
            removeEventListener(Event.ADDED_TO_STAGE, _initialize);

            var sw : Number = stage.stageWidth;
            var sh : Number = stage.stageHeight;

            // ----------------------------------
            // BG
            // ----------------------------------
            addChild(new Bitmap(new BitmapData(sw, sh, false, 0x000000))) as Bitmap;

            // ----------------------------------
            // perlin noize
            // ----------------------------------
            offsets = [new Point(), new Point()];
            noizBmd = new BitmapData(256, 256, false);
            
            // ----------------------------------
            // bitmap
            // ----------------------------------
            bmd = new BitmapData(256, 256, true, 0xffffffff);
            bm = addChild(new Bitmap(bmd)) as Bitmap ;
            bm.x = sw >> 1 - bm.width >> 1;
            bm.y = sh >> 1 - bm.height >> 1;
            bm.visible = false;
            stage.addEventListener(MouseEvent.CLICK, onSmoke_Handler);
            
            //----------------------------------
            //  text field
            //----------------------------------
            tf = addChild(new TextField()) as TextField;
            tf.defaultTextFormat = new TextFormat(null, 16, 0xffffff);
            tf.autoSize = TextFieldAutoSize.LEFT;
            tf.text = "CLICK TO STAGE!";
            tf.mouseEnabled = false;
            tf.x = (sw >> 1) - (tf.width >> 1) ;
            tf.y = ( sh * 0.9 ) - ( tf.height >> 1) ;
        }

        private function onSmoke_Handler(event : MouseEvent) : void
        {
            if (bm.visible ) return ;
            bm.x = stage.mouseX - (bm.width >> 1);
            bm.y = stage.mouseY - (bm.height >> 1);
            bm.visible = true;
            
            smokeAlpha = 0.1;

            var fadeOutTime : Number =0.6;// 0.4 * Math.random() + 0.3;
            
            Tween24.serial(
                 Tween24.tween(this, fadeOutTime*0.1, Ease24._1_SineInOut, {"smokeAlpha":0.9})
                ,Tween24.tween(this, fadeOutTime*0.9, Ease24._2_QuadIn, {"smokeAlpha":0})
            )
            .onUpdate(onUpdate)
            .onComplete(onComplete)
            .play();
        }

        private function onUpdate() : void
        {
            var i : int;
            var max : int = offsets.length;
            for (i = 0; i < max; i++) {
                var p : Point = offsets[i] as Point;
                var direc : int = (i % 2 == 0) ? 1 : -1;
                p.x += (SPEED * (i + 1) * direc)>>1;
                p.y += (SPEED * (i + 1) * direc);
            }
            noizBmd.perlinNoise(64, 64, offsets.length, SEED, false, true, BitmapDataChannel.ALPHA, true, offsets);
          
            noizBmd.draw(getGarad(smokeAlpha), null, null, BlendMode.ALPHA);
            noizBmd.draw(noizBmd, null, null, BlendMode.ADD);
             noizBmd.draw(noizBmd, null, null, BlendMode.ADD); 
            bmd.fillRect(RECT, 0xffffffff);
            bmd.copyChannel(noizBmd, RECT, P, BitmapDataChannel.RED, BitmapDataChannel.ALPHA);
        }

        private function onComplete() : void
        {
            bm.visible = false;
        }

        /**
         *  circle gradation
         */
        private function getGarad(smokeAlpha : Number) : Shape
        {
            if (smokeAlpha > 1 ) smokeAlpha = 1;
            if (smokeAlpha < 0 ) smokeAlpha = 0;
            
            var smokeKey:String = int(smokeAlpha*100).toString();
            if (!gDic) gDic = new Dictionary();
            if (!gDic[smokeKey]) {
                var  grad : Shape = new Shape();
                var gradMrx : Matrix = new Matrix();
                gradMrx.createGradientBox(256, 256, Math.PI / 2, 0, 0);
                var g : Graphics = grad.graphics;
                g.beginGradientFill(GradientType.RADIAL
                    , gColor
                    , [smokeAlpha, 0]
                    , gAlpha
                    , gradMrx
                    , SpreadMethod.PAD
                );
                g.drawRect(0, 0, 256, 256);
                gDic[smokeKey] = grad;
            }
            return gDic[smokeKey] ;
        }
    }
}