forked from: Particle Sample
大量のパーティクルを発生させてみた
マウスを押してる間でてくるよ
// forked from cellfusion's Particle Sample
// write as3 code here..
// 大量のパーティクルを発生させてみた
// マウスを押してる間でてくるよ
package
{
import flash.display.Sprite;
import flash.display.StageQuality;
import flash.events.Event;
import flash.events.MouseEvent;
import net.hires.debug.Stats;
[SWF(width="465", height="465", backgroundColor="0x000000", frameRate="30")]
public class ParticleSample extends Sprite
{
private var _particles:Array = [];
private var _emitter:Emitter;
private var _emitter2:Emitter;
// 1フレーム間に発生させる Particle 数
private const PARTICLE_NUM:uint = 4;
public function ParticleSample()
{
stage.quality = StageQuality.BEST;
setup();
addChild(new Stats());
}
private function setup():void
{
_emitter = new Emitter();
_emitter2 = new Emitter();
addChild(_emitter);
addChild(_emitter2);
for (var i:uint = 0; i < 10000; i++) {
_particles.push(new Particle());
}
addEventListener(Event.ENTER_FRAME, draw);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
}
private function draw(event:Event):void
{
_emitter.update();
_emitter2.update(_emitter);
for each (var p:Particle in _particles) {
if (!p.destroy) {
//p.vx += 0.64;
//p.vy -= .24;
p.update();
}
}
}
private function mouseDown(event:MouseEvent):void
{
addEventListener(Event.ENTER_FRAME, createParticle);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
}
private function mouseUp(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
removeEventListener(Event.ENTER_FRAME, createParticle);
}
private function createParticle(event:Event):void
{
var count:uint = 0;
for each (var p:Particle in _particles) {
// 停止している Particle を探す
if (p.destroy) {
if((Math.random() * 2) >> 0)
{
p.x = _emitter.x;
p.y = _emitter.y;
p.init();
}
else
{
p.x = _emitter2.x;
p.y = _emitter2.y;
p.init(.4);
}
addChild(p);
count++;
}
if (count > PARTICLE_NUM) break;
}
}
}
}
import flash.display.Sprite;
class Emitter extends Sprite
{
public var vx:Number = 0;
public var vy:Number = 0;
public var ax:Number = 0;
public var ay:Number = 0;
private const DT:Number = .04;
private const DT2:Number = DT * DT;
public function Emitter()
{
//graphics.beginFill(0x808080);
//graphics.drawCircle(0, 0, 10);
//graphics.endFill();
}
public function update($emitter:Emitter = null):void
{
var ax:Number;
var ay:Number;
if($emitter == null)
{
ax = (root.mouseX - x) * 120;
ay = (root.mouseY - y) * 120;
}
else
{
ax = ($emitter.x - x) * 64;
ay = ($emitter.y - y) * 64;
}
vx += ax * DT;
vy += ay * DT;
vx *= 0.9;
vy *= 0.9;
x += vx * DT + .5 * ax * DT2;
y += vy * DT + .5 * ay * DT2;
ax = ay = 0;
}
}
import flash.filters.BlurFilter;
class Particle extends Sprite
{
public var vx:Number;
public var vy:Number;
public var life:Number;
public var defSize:Number;
public var size:Number;
public var color:Number;
public var inc:int;
private var _count:uint;
private var _destroy:Boolean;
public function Particle()
{
size = Math.random() * 9 + 9;
defSize = size;
changeColor(Math.random() * 80, Math.random() * 155, Math.random() * 155 + 100);
drawGraphics();
//filters = [new BlurFilter(4,4,1)];
// 大量のオブジェクトを重ねるとおかしくなる
blendMode = "add";
_destroy = true;
}
private function drawGraphics():void
{
graphics.clear();
graphics.beginFill(color);
graphics.drawCircle(0, 0, size);
graphics.endFill();
}
private function changeColor($r:uint, $g:uint, $b:uint):void
{
var red:uint = Math.floor($r);
var green:uint = Math.floor($g);
var blue:uint = Math.floor($b);
color = (red << 16) | (green << 8) | (blue);
}
public function init($k:Number=1):void
{
vx = Math.random() * 30 - 15;
vy = Math.random() * 2 - 1;
life = Math.random() * 60 + 20;
_count = 0;
alpha = 1 * $k;
size = defSize;
changeColor(Math.random() * 80 / $k, Math.random() * 155, Math.random() * 155 + 100);
drawGraphics();
inc = ((Math.random() * 2) >> 0);
_destroy = false;
}
public function update():void
{
x += vx;
y += vy;
alpha -= .024 / Math.abs(inc);
_count++;
size += inc
drawGraphics();
// 死亡フラグ
if (life < _count) {
_destroy = true;
parent.removeChild(this);
}
}
public function get destroy():Boolean
{
return _destroy;
}
}