package
{
import flash.display.Sprite;
import flash.display.*
import flash.events.Event;
import flash.filters.DisplacementMapFilter;
import flash.geom.*
/**
* @author:Frost.Yen
* @E-mail:871979853@qq.com
* @create: 2016-6-28 下午3:37:51
*
*/
[SWF(width="1024",height="768")]
public class FireEffect extends Sprite
{
private var bmd:BitmapData;
private var target:Sprite = createSharp(400, 30,0);
private var speed:Number = Math.random() * 4 + 4;
private var strengh:Number = Math.random() * 60 + 60;
private var size:Number = Math.random() * 17 + 20;
private var randomseed:Number = Math.floor(Math.random() * 10);
private var offset:Point = new Point();
private var v:Point;
private var w:Number = 50;
public function FireEffect()
{
addChild(target);
addTo(target);
target.x = 0;
target.y = 200;
}
private function createSharp(width:Number,height:Number,angle:Number):Sprite {
var s:Sprite = new Sprite();
s.graphics.beginFill(0, 0);
s.graphics.drawRect( -w, -w, width + 2 * w, w);
s.graphics.beginFill(0xffffff);
s.graphics.drawRect( -w, height, width + 2 * w, w);
var fillType:String = GradientType.LINEAR;
var colors:Array = [0x900000, 0x900000,0xff9900,0xfffd8a,0xffffff];
var alphas:Array = [0,1,1,1, 1];
var ratios:Array = [0x00,0xff/16,0xff*2/6,0xff*2/3, 0xFF];
var matr:Matrix = new Matrix();
matr.createGradientBox(width, height, Math.PI / 2);
var spreadMethod:String = SpreadMethod.PAD;
s.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);
s.graphics.drawRect(0, 0, width, height);
return s;
}
public function addTo(target:Object):void {
bmd = new BitmapData(target.width, target.height);
v = new Point(Math.random() * (speed / 4) - speed / 8,Math.random() * (speed / 4) + speed / 4 * 3);
target.addEventListener(Event.ENTER_FRAME, update);
}
private function update(e:Event):void {
target.filters = [new DisplacementMapFilter(bmd, new Point(), BitmapDataChannel.RED,BitmapDataChannel.RED, 10, strengh, "clamp")];
offset=offset.add(v);
bmd.perlinNoise(size / 2, size, 3, randomseed, false, true, 1, true, [offset, offset]);
}
}
}