forked from: ExEmitPanda
EmitPanda
/**
* Copyright bkzen ( http://wonderfl.net/user/bkzen )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ci2w
*/
// forked from clockmaker's ExEmitPanda
////////////////////////////////////////////////////////////////////////////////
// EmitPanda
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite
import flash.events.Event;
import flash.system.LoaderContext;
import flash.system.Security;
import net.hires.debug.Stats;
import net.wonderfl.utils.ClassLoader;
[SWF(backgroundColor="#FFFFFF",width="465",height="465",frameRate="60")]
public class PandaEmitter extends Sprite {
private static const PATH:String = "http://www.project-nya.jp/images/flash/panda2d.swf";
private var emit:EmitParticle;
public function PandaEmitter(){
Security.allowDomain("www.project-nya.jp");
var loader: ClassLoader= new ClassLoader();
loader.addEventListener(ClassLoader.CLASS_LOADED, completeHandler);
loader.load(PATH, new LoaderContext(true));
}
private function completeHandler(evt:Event):void {
evt.target.removeEventListener(ClassLoader.CLASS_LOADED, arguments.callee);
//Pandaクラス
emit = new EmitParticle(ClassLoader(evt.target).getClass("jp.nya.project.character.Panda"));
addChild(emit);
addChild(new Stats());
mouseChildren = false;
mouseEnabled = false;
}
}
}
import flash.display.*;
import flash.events.*;
import flash.geom.*;
/** EmitParticleクラス */
internal final class EmitParticle extends Sprite {
private static const MAX_NUM:uint = 3000;
private static const EMIT_NUM:Number = 30;
private static const GRAVITY:Number = 0.5;
private static const FRICTION:Number = 0.98;
private var first: Particle;
private var count:int = 0;
private var orijinal:BitmapData;
private var canvas: BitmapData;
private var drawPoint: Point;
public function EmitParticle(panda:Class){
var disp:DisplayObject = new panda();
// 元絵
orijinal = new BitmapData(disp.width, disp.height, true, 0x0);
orijinal.draw(disp, new Matrix(1, 0, 0, 1, 30, 60));
// キャンバス
canvas = new BitmapData(465, 465, false, 0xFFFFFF);
addChild(new Bitmap(canvas));
// オブジェクトプール
var p: Particle = first = new Particle();
for (var i:int = 0; i < MAX_NUM; i++){
p = p.next = new Particle();
}
//p.next = first;
drawPoint = new Point();
addEventListener(Event.ENTER_FRAME, update);
}
private function update(evt: Event): void
{
var p: Particle = first, i: int;
canvas.lock();
canvas.fillRect(canvas.rect, 0xFFFFFF);
while (p = p.next)
{
if (p.enable)
{
p.x += p.vx;
p.y += p.vy;
if (p.y > 465)
{
p.enable = false;
}
else
{
p.vx * FRICTION;
p.vy * FRICTION;
p.vy += GRAVITY;
drawPoint.x = p.x;
drawPoint.y = p.y;
canvas.copyPixels(orijinal, orijinal.rect, drawPoint, null, null, true);
}
}
else
{
if (i < EMIT_NUM) emit(p), i++;
}
}
canvas.unlock();
}
private function emit(p:Particle):void
{
p.vx = 20 * (Math.random() - 0.5);
p.vy = -20 * (Math.random() - 0.5);
p.x = mouseX, p.y = mouseY;
p.enable = true;
}
}
/** Particleクラス */
internal final class Particle {
public var x:Number = 0;
public var y:Number = 0;
public var vx:Number = 0;
public var vy: Number = 0;
public var enable: Boolean = false;
public var next: Particle;
}