爆発エフェクト(クリックしたとこで爆発します)
爆発のエフェクト。
@author SIBA
// write as3 code here..
package {
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.MouseEvent;
[SWF(width=800, height=600, backgroundColor=0xAADDFF)]
/**
* 爆発のエフェクト。
* @author SIBA
*/
public class Main04 extends Sprite {
public function Main04() {
stage.addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(event:MouseEvent):void {
var burst:Burst = new Burst();
burst.x = event.stageX - burst.width/2;
burst.y = event.stageY - burst.height/2;
addChild(burst);
burst.addEventListener(BurstEvent.COMPLETE, onFinishBurst);
}
private function onFinishBurst(event:BurstEvent):void {
removeChild(event.currentTarget as DisplayObject);
event.currentTarget.removeEventListener(BurstEvent.COMPLETE, onFinishBurst);
}
}
}
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.Graphics;
import flash.filters.BlurFilter;
import flash.events.Event;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.display.BlendMode;
class Burst extends Bitmap {
private var particleDesign:Sprite = new Sprite();
private var particleList:Array = [];
public function Burst() {
var tempBmp:BitmapData = new BitmapData(200, 200, true, 0x00000000);
super(tempBmp);
// パーティクルのデザインを作成する
createParticleDesign();
// パーティクルを作成する
createParticle();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function createParticleDesign():void {
// 円を描く
const g:Graphics = particleDesign.graphics;
g.beginFill(0xFF8022, 0.5);
g.drawCircle(0, 0, 10);
g.endFill();
// ぼかし効果を適用する
particleDesign.filters = [new BlurFilter(10, 10, 1)];
}
private function createParticle():void {
for (var i:int=0; i<50; i++) {
var x:int = bitmapData.width/2;
var y:int = bitmapData.height/2;
var v:Number = Math.random()*5;
var angle:uint = Math.random()*360;
var ax:Number = 0;
var ay:Number = 0;
var enagy:Number = 1;
var particle:Particle = new Particle(x, y, v, angle, ax, ay, enagy);
particleList[particleList.length] = particle;
}
}
private function onEnterFrame(event:Event):void {
bitmapData.colorTransform(bitmapData.rect, new ColorTransform(1, 1, 1, 1, 0, 0, 0, -32));
for (var i:int=particleList.length-1; i>-1; i--) {
var particle:Particle = Particle(particleList[i]);
particle.move();
var mat:Matrix = new Matrix();
mat.translate(particle.x, particle.y);
bitmapData.draw(particleDesign, mat, new ColorTransform(1, 1, 1, particle.enagy), BlendMode.ADD);
if (particle.enagy < 0.01) {
particleList.splice(i, 1);
}
}
if (particleList.length <= 0) {
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
dispatchEvent(new BurstEvent(BurstEvent.COMPLETE));
}
}
}
class BurstEvent extends Event {
public static const COMPLETE:String = "complete";
public function BurstEvent(type:String) {
super(type);
}
}
class Particle {
public var x:Number;
public var y:Number;
private var vx:Number;
private var vy:Number;
private var ax:Number;
private var ay:Number
public var enagy:Number;
public function Particle(x:int, y:int, v:Number, angle:uint, ax:Number, ay:Number, enagy:Number) {
this.x = x;
this.y = y;
this.vx = Math.cos(angle*Math.PI/180) * v;
this.vy = Math.sin(angle*Math.PI/180) * v;
this.ax = ax;
this.ay = ay;
this.enagy = enagy;
}
public function move():void {
x += vx;
y += vy;
vx += ax;
vy += ay;
enagy *= 0.9;
}
}