forked from: 2009-2-26 爆発エフェクト
// forked from hikipuro's 2009-2-26 爆発エフェクト
package
{
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BitmapFilterQuality;
import flash.filters.BlurFilter;
import flash.geom.Point;
[SWF(width="500", height="500", backgroundColor="0x000000", frameRate="60")]
public class Main extends Sprite
{
private var particles:Array;
private var output:BitmapData;
private var blur:BlurFilter;
private var mouseDownFlag:Boolean;
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, initialize);
}
private function initialize(e:Event=null):void
{
removeEventListener(e.type, arguments.callee);
mouseDownFlag = false;
particles = new Array();
blur = new BlurFilter(2, 2, BitmapFilterQuality.LOW);
output = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0);
addChild(new Bitmap(output));
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onMouseDown(event:Event):void
{
mouseDownFlag = true;
}
private function onMouseUp(event:Event):void
{
mouseDownFlag = false;
}
private function onEnterFrame(event:Event):void
{
if (mouseDownFlag)
{
addParticle(mouseX, mouseY);
}
render();
}
public function addParticle(x: Number, y: Number):void
{
for (var i:int = 0; i < 30; i++)
{
var len: Number,
angle: Number;
len = Math.random() * 10;
angle = Math.random() * Math.PI * 2;
particles.push(new Particle(x, y, len, angle));
}
}
public function render():void
{
var deleteArray:Array = new Array();
output.lock();
for each (var particle:Particle in particles)
{
particle.draw(output);
if (!particle.isAlive)
{
var i:int = particles.indexOf(particle);
particles.splice(i, 1);
}
}
output.applyFilter(output, output.rect, new Point(), blur);
output.unlock();
}
}
}
import flash.display.BitmapData;
import flash.geom.Point;
class Particle
{
private var _x:Number;
private var _y:Number;
private var _vx:Number;
private var _vy:Number;
private var _energy:Number;
private var _life:int;
/**
* コンストラクタ
*/
public function Particle(x:Number, y:Number, len:Number=0, angle:Number=0)
{
//angle = (angle / 180) * Math.PI;
var p:Point = Point.polar(len, angle);
_x = x;
_y = y;
//_sx = Math.cos(angle) * strength;
//_sy = Math.sin(angle) * strength;
_vx = p.x;
_vy = p.y;
_energy = 1;
_life = 120;
}
/**
* 実際に書き込む。
*/
public function draw(buffer:BitmapData):void
{
var color:uint = (_energy * 0xff) << 16;
buffer.setPixel(_x, _y, color);
_x += _vx;
_y += _vy;
// 必要な値を減衰
_vx *= .98;
_vy *= .98;
_energy *= .98;
_life--;
}
/**
* 生存チェック
*/
public function get isAlive():Boolean
{
return _life > 0;
}
}