Advertisement
durss

Untitled

Sep 16th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.muxxu.mush.contaminator.components {
  2.     import com.nurun.core.lang.Disposable;
  3.     import com.nurun.utils.math.MathUtils;
  4.  
  5.     import flash.display.Bitmap;
  6.     import flash.display.BitmapData;
  7.     import flash.display.BitmapDataChannel;
  8.     import flash.display.InterpolationMethod;
  9.     import flash.display.Shape;
  10.     import flash.display.SpreadMethod;
  11.     import flash.display.Sprite;
  12.     import flash.events.Event;
  13.     import flash.filters.BlurFilter;
  14.     import flash.geom.Matrix;
  15.     import flash.geom.Point;
  16.    
  17.     /**
  18.      *
  19.      * @author Poil
  20.      * @date 5 sept. 2012;
  21.      */
  22.     public class Fog extends Sprite {
  23.         private var _p1:Point;
  24.         private var _p2:Point;
  25.         private var _bmd:BitmapData;
  26.         private var _holder:Sprite;
  27.         private var _maskMc:Shape;
  28.         private var _seed:Number;
  29.        
  30.        
  31.        
  32.        
  33.         /* *********** *
  34.          * CONSTRUCTOR *
  35.          * *********** */
  36.         /**
  37.          * Creates an instance of <code>Fog</code>.
  38.          */
  39.         public function Fog() {
  40.             addEventListener(Event.ADDED_TO_STAGE, initialize);
  41.         }
  42.  
  43.        
  44.        
  45.         /* ***************** *
  46.          * GETTERS / SETTERS *
  47.          * ***************** */
  48.         override public function get height():Number {
  49.             return 200;
  50.         }
  51.  
  52.  
  53.  
  54.         /* ****** *
  55.          * PUBLIC *
  56.          * ****** */
  57.  
  58.         public function stop():void {
  59.             removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
  60.             while(numChildren > 0) {
  61.                 if(getChildAt(0) is Disposable) Disposable(getChildAt(0)).dispose();
  62.                 removeChildAt(0);
  63.             }
  64.             _bmd.dispose();
  65.             _holder.mask = null;
  66.         }
  67.  
  68.  
  69.        
  70.        
  71.         /* ******* *
  72.          * PRIVATE *
  73.          * ******* */
  74.         /**
  75.          * Initialize the class.
  76.          */
  77.         private function initialize(event:Event):void {
  78.             removeEventListener(Event.ADDED_TO_STAGE, initialize);
  79.             _p1 = new Point();
  80.             _p2 = new Point();
  81.             _bmd = new BitmapData(150, 400, true, 0);
  82.             _maskMc = addChild(new Shape()) as Shape;
  83.             _seed = MathUtils.randomNumberFromRange(0, 99999999999999);
  84.            
  85.             var m:Matrix = new Matrix();
  86.             m.createGradientBox(_bmd.width, _bmd.height, Math.PI*.5);
  87.             _maskMc.graphics.beginGradientFill("linear", [0xff0000,0xff0000], [0,.5], [0,0x5f], m, SpreadMethod.PAD, InterpolationMethod.RGB);
  88.             _maskMc.graphics.drawRect(0, 0, _bmd.width, _bmd.height);
  89.             _maskMc.graphics.endFill();
  90.            
  91.             _holder = addChild(new Sprite()) as Sprite;
  92.             _holder.addChild(new Bitmap(_bmd));
  93.            
  94.             _holder.cacheAsBitmap = true;
  95.             _maskMc.cacheAsBitmap = true;
  96.             _holder.mask = _maskMc;
  97.            
  98.             //*
  99.             _holder.width = stage.stageWidth;
  100.             _holder.height = height;
  101.             //*/
  102.             _maskMc.x = _holder.x;
  103.             _maskMc.y = _holder.y;
  104.             _maskMc.width = _holder.width;
  105.             _maskMc.height = _holder.height;
  106.            
  107.             addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  108.         }
  109.        
  110.         private function enterFrameHandler(event:Event):void {
  111.             _p1.x += .5;
  112.             _p2.x -= .5;
  113.             _bmd.fillRect(_bmd.rect, 0);
  114.             _bmd.perlinNoise(200, 200, 2, _seed, true, true, BitmapDataChannel.ALPHA, true, [_p1, _p2]);
  115.             _bmd.threshold(_bmd, _bmd.rect, new Point(), "<", 0x777777, 0, 0xffffff, true);
  116.             _bmd.applyFilter(_bmd, _bmd.rect, new Point(), new BlurFilter(30,30,2));
  117.         }
  118.        
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement