as3创建火焰效果代码

作者:袖梨 2022-06-28

fireeffect.as文件

 代码如下 复制代码

package effects
{
 import flash.display.*
 import flash.events.Event;
 import flash.filters.DisplacementMapFilter;
 import flash.geom.*
 /**
  * ...
  * @author sliz
  */
 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]);
  }
 }
 
}


main.as文件

 代码如下 复制代码
package
{
 import flash.display.Sprite;
 import effects.FireEffect;
 import flash.events.MouseEvent;
 import flash.net.navigateToURL;
 import flash.net.URLRequest;
 import flash.text.TextField;
 
 /**
  * ...
  * @author sliz
  */
 public class Main extends Sprite
 {
  public function Main():void {
   addChild(new FireEffect());
   var lable:TextField = new TextField();
   lable.textColor = 0xffffff;
   lable.selectable = false;
   lable.text = "sliz";
   lable.addEventListener(MouseEvent.CLICK, function():void { navigateToURL(new URLRequest("http://www.111com.net/")); } );
   addChild(lable);
   lable.x = 370;
   lable.y = 380;
  }
 }
 
}

flash文件中

 代码如下 复制代码

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]);
}

相关文章

精彩推荐