In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

使用例006 - 適当パーティクルエミッター0.6(未完成

Get Adobe Flash player
by esukei 17 Feb 2012
/**
 * Copyright esukei ( http://wonderfl.net/user/esukei )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/cZdE
 */

// forked from esukei's 適当パーティクルエミッター0.6(未完成

// forked from esukei's forked from: 適当パーティクルエミッター0.4(未完成
// forked from esukei's 適当パーティクルエミッター0.4(未完成
// forked from esukei's forked from: forked from: forked from: forked from: 適当パーティクルエミッター0.2(未完成
// forked from esukei's forked from: forked from: forked from: 適当パーティクルエミッター0.2(未完成
// forked from esukei's forked from: forked from: 適当パーティクルエミッター0.2(未完成
// forked from esukei's forked from: 適当パーティクルエミッター0.2(未完成
// forked from esukei's 適当パーティクルエミッター0.2(未完成
// forked from esukei's forked from: 適当パーティクルエミッター(未完成
// forked from esukei's 適当パーティクルエミッター(未完成
package {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import net.hires.debug.Stats;
	[SWF(frameRate="60", backgroundColor="0")]
	public class FlashTest extends Sprite {
		
		public var emitter:Emitter;
		public var emitter2:Emitter;
		
		public var canvas:BitmapData;
		
		private var iEmitters:uint = 0;
		private var emittersLength:uint = 0;
		
		private var tmpEmitter:Emitter;
		
		private var iParticles:uint = 0;
		private var particlesLength:uint = 0;
		
		private var console:Console;
		
		private var stats:Stats;
		public var particle:Particle;
		
		public function FlashTest() {
			// write as3 code here..
			//emitter = new Emitter();
			
			particle = new Particle();
			
			emitter =  new Emitter(particle, 0,0,0,0,0,-.02,  1,  1, 0,.5, 5, 100, 100,.2);
			emitter2 = new Emitter(emitter,  0,0,0,0,0, .1, .98,.98, 5,10, 10, 300,   0,0);
			
			
			canvas = new BitmapData(465, 465, true, 0x00000000);
			addChild(new Bitmap(canvas));
			
			console = new Console();
			addChild(console);
			
			addEventListener(MouseEvent.CLICK, function(e:Event):void
			{
				emitter2.x = stage.mouseX;
				emitter2.y = stage.mouseY;
				emitter2.emit();
			});
			
			addEventListener(Event.ENTER_FRAME,function(e:Event):void
			{
				
				emitter2.x = stage.mouseX;
				emitter2.y = stage.mouseY;
				//emitter.emit();
				
				console.clear();
				console.log('update');
				
				//console.log('previous Particles:' + String(emitter2.particles.length));
				
				emitter2.update();
				
				//console.log('current Particles:' + String(emitter2.particles.length));
				emittersLength = emitter2.particles.length;
				
				canvas.lock();
				canvas.fillRect(new Rectangle(0,0,465,465), 0x00000000);
				
				for(iEmitters = 0; iEmitters < emittersLength; iEmitters++){
					
					tmpEmitter = Emitter(emitter2.particles[iEmitters]);
					tmpEmitter.emit();
tmpEmitter.update();


					particlesLength = tmpEmitter.particles.length;
//canvas.setPixel32(tmpEmitter.x, tmpEmitter.y, 0xFFFF0000);

//console.log('current Particles:' + String(particlesLength));
					for(iParticles = 0; iParticles < particlesLength; iParticles++)
					{
						var r:Number = tmpEmitter.particles[iParticles].lifeTime / tmpEmitter.particles[iParticles].maxLifeTime;
						canvas.setPixel32(tmpEmitter.particles[iParticles].x, tmpEmitter.particles[iParticles].y, r * 255 << 24 | 255 << 16 | r * r * r * 255 << 8 | r * r * r * r * 255);
						//canvas.setPixel32(tmpEmitter.particles[iParticles].x, tmpEmitter.particles[iParticles].y, 0xFFFFFFFF);
					}
				}
				canvas.unlock();
			});
			
			stats = new Stats();
			stats.x = 400;
			addChild(stats);
		}
	}
}

import flash.events.EventDispatcher;
class Particle extends EventDispatcher {
	
	public var x:Number;
	public var y:Number;
	public var vx:Number;
	public var vy:Number;
	public var ax:Number;
	public var ay:Number;
	public var rx:Number;
	public var ry:Number;
	public var maxLifeTime:uint;
	public var lifeTime:uint;
	
	public function Particle(x:Number = 0, y:Number = 0, vx:Number = 0, vy:Number = 0, ax:Number = 0, ay:Number = 0, rx:Number = 1.0, ry:Number = 1.0, lifeTime:uint = 1)
	{
		this.x = x;
		this.y = y;
		this.vx = vx;
		this.vy = vy;
		this.ax = ax;
		this.ay = ay;
		this.rx = rx;
		this.ry = ry;
		this.lifeTime = this.maxLifeTime =  lifeTime;
	}
	
	public function clone(x:Number = 0, y:Number = 0, vx:Number = 0, vy:Number = 0, ax:Number = 0, ay:Number = 0, rx:Number = 1.0, ry:Number = 1.0, lifeTime:uint = 1):Particle
	{
		return new Particle(x, y, vx, vy, ax, ay, rx, ry, lifeTime);
	}
	
}

import flash.display.BitmapData;
import flash.geom.Point;
import flash.geom.Rectangle;
class Emitter extends Particle {
	
	//public var x:Number;
	//public var y:Number;
	//public var vx:Number = 0;
	//public var vy:Number = 0;
	public var ivx:Number;
	public var ivy:Number;
	public var tivx:Number = 0;
	public var tivy:Number = 0;
	public var pivx:Number = 0;
	public var pivy:Number = 0;
	public var ivr:Number = 1;
	public var px:Number;
	public var py:Number;
	public var pvx:Number;
	public var pvy:Number;
	public var pax:Number;
	public var pay:Number;
	public var prx:Number;
	public var pry:Number;
	
	public var particle:Particle;
	
	public var particles:Array;
	public var plifeTime:uint;
	public var lifeTimeRandom:int;
	public var amounts:uint;
	public var motionInfluence:Number;
	
	public var explosion:Number;
	public var vibration:Number;
	
	public var velocityMin:Number = 0;
	
	public var nozzle:BitmapData;
	
	public var nozzleType:String = 'TRAIL';
	
	private var trailNozzle:Vector.<EmitPoint>;
	
	public var autoEmit:Boolean;
	
	public function Emitter(particle:Particle, x:Number = 0, y:Number = 0, pvx:Number = 0, pvy:Number = 0, pax:Number = 0, pay:Number = 0, prx:Number = 1.0, pry:Number = 1.0, explosion:Number = 0.0, vibration:Number = 0.0, amounts:uint = 1, plifeTime:uint = 1, lifeTimeRandom:int = 0, motionInfluence:Number = 0.0, autoEmit:Boolean = false)
	{
		this.x = this.px = x;
		this.y = this.py = y;
		this.pvx = pvx;
		this.pvy = pvy;
		this.pax = pax;
		this.pay = pay;
		this.prx = prx;
		this.pry = pry;
		this.explosion = explosion;
		this.vibration = vibration;
		this.plifeTime = plifeTime;
		this.amounts = amounts;
		this.motionInfluence = motionInfluence;
		this.lifeTimeRandom = lifeTimeRandom;
		
		this.autoEmit = autoEmit;
		
		this.particle = particle;
		
		particles = new Array();
		
		nozzle = new BitmapData(100,100);
		
		trailNozzle = new Vector.<EmitPoint>
			
		if(autoEmit)
		{
			//this.addEventListener(Event.ENTER_FRAME, loop);
		}
	}
	public function loop(event:Event):void
	{
		emit();
		update();
	}
	
	private var clonedEmitter:Emitter;
	override public function clone(x:Number=0, y:Number=0, vx:Number=0, vy:Number=0, ax:Number=0, ay:Number=0, rx:Number=1.0, ry:Number=1.0, lifeTime:uint=1):Particle
	{
		clonedEmitter = new Emitter(particle, x, y, pvx, pvy, pax, pay, prx, pry, explosion, vibration, amounts, plifeTime, lifeTimeRandom, motionInfluence, true);
		clonedEmitter.vx = vx;
		clonedEmitter.vy = vy;
		clonedEmitter.ax = ax;
		clonedEmitter.ay = ay;
		clonedEmitter.rx = rx;
		clonedEmitter.ry = ry;
		clonedEmitter.lifeTime = clonedEmitter.maxLifeTime = lifeTime;
		return clonedEmitter;
	}
	
	private var iEmit:uint = 0;
	
	private var r:Number = 0;
	private var evx:Number = 0;
	private var evy:Number = 0;
	private var vibx:Number = 0;
	private var viby:Number = 0;
	private var rv:Number = 0;
	private var ta:Number = 0;
	
	private var e:Number = 0;
	private var ttx:Number = 0;
	private var tty:Number = 0;
	
	private var ti:Number = 0;
	private var tj:Number = 0;
	
	private var lx:Number = 0;
	private var ly:Number = 0;
	
	private var sx:Number = 0;
	private var sy:Number = 0;
	
	private var tvx:Number = 0;
	private var tvy:Number = 0;
	
	private var tmpx:Number = 0;
	private var tmpy:Number = 0;
	private var tmpP:uint = 0;
	
	public function emit():void
	{
		tvx = x - px;
		tvy = y - py;
		//vx,vy not correct?
		ivx = tvx * motionInfluence;
		ivy = tvy * motionInfluence;
		
		ta = amounts;//(Math.sqrt(vx * vx + vy * vy) > velocityMin) ? amounts : 0;
		
		//Make Trail Nozzle Vector
		tvx = x - px;
		tvy = y - py;
		
		lx = Math.abs(tvx);
		ly = Math.abs(tvy);
		
		sx = (lx != 0) ? (tvx) / lx : 1;
		sy = (ly != 0) ? (tvy) / ly : 1;
		
		ttx = 2 * lx;
		tty = 2 * ly;
		
		ti = 1;
		tj = 0;
		
		if(lx >= ly)
		{
			trailNozzle = new Vector.<EmitPoint>(lx,true);
			e = lx;
			
			for(ti; ti <= lx; ti++)
			{
				e += tty;
				if(e >= ttx)
				{
					tj++;
					e = e - ttx;
				}
				trailNozzle[ti-1] = new EmitPoint(sx * ti, sy * tj);
			}
		}
		else
		{
			trailNozzle = new Vector.<EmitPoint>(ly,true);
			e = ly;
			
			for(ti; ti <= ly; ti++)
			{
				e += ttx;
				if(e >= tty)
				{
					tj++;
					e = e - tty;
				}
				trailNozzle[ti-1] = new EmitPoint(sx * tj, sy * ti);
			}
		}
		
		for(iEmit = 0; iEmit < ta; iEmit++)
		{
			r = 360 * iEmit / amounts * Math.PI / 180;
			evx = Math.cos(r) * explosion;
			evy = Math.sin(r) * explosion;
			
			r = 2 * Math.random() * Math.PI;//360 * Math.random() * Math.PI / 180
			rv = vibration * Math.random();
			
			vibx = rv * Math.sin(r);           
			viby = rv * Math.cos(r);
			
			if(trailNozzle.length > 0)
			{
				tmpP = uint((trailNozzle.length-1) * Math.random());
				//trailNozzle[0];
				tmpx = trailNozzle[tmpP].x + px;
				tmpy = trailNozzle[tmpP].y + py;
				
				
				ivr = (tmpP + 1) / trailNozzle.length;
				tivx = ivx * ivr + pivx * (1 - ivr);
				tivy = ivy * ivr + pivy * (1 - ivr);
			}
			else
			{
				tmpx = x;
				tmpy = y;
			}
			particles.push(particle.clone(tmpx+vibx, tmpy+viby, pvx + tivx + evx + vibx , pvy + tivy + evy + viby, pax, pay, prx, pry, plifeTime - int(Math.random() * lifeTimeRandom)));
			//particles.push(new Particle(tmpx, tmpy, pvx + ivx + evx + vibx , pvy + ivy + evy + viby, pax, pay, prx, pry, lifeTime - int(Math.random() * lifeTimeRandom)));
			
			//particles.push(new Particle(x, y, pvx + ivx + evx + vibx , pvy + ivy + evy + viby, pax, pay, prx, pry, lifeTime - int(Math.random() * lifeTimeRandom)));
			
			//particles.push(new Particle(int(nozzle.width * Math.random()) + x, (nozzle.height * Math.random()) + y, pvx + ivx + evx + vibx , pvy + ivy + evy + viby, pax, pay, prx, pry, lifeTime - int(Math.random() * lifeTimeRandom)));
		}
		pivx = ivx;
		pivy = ivy;
	}
	
	private var iUpdate:int = 0;
	private var pLength:int = 0;
	public function update():void
	{
		vx = x - px;
		vy = y - py;
		px = x;
		py = y;
		
		pLength = particles.length;
		if(pLength == 0) return;
		
		iUpdate = pLength - 1;
		do
		{
			particles[iUpdate].vx += particles[iUpdate].ax;
			particles[iUpdate].vx *= particles[iUpdate].rx;
			particles[iUpdate].x += particles[iUpdate].vx;
			
			particles[iUpdate].vy += particles[iUpdate].ay;
			particles[iUpdate].vy *= particles[iUpdate].ry;
			particles[iUpdate].y += particles[iUpdate].vy;
			
			if(particles[iUpdate].lifeTime <= 0)
			{
				particles.splice(iUpdate,1);
				continue;
			}
			particles[iUpdate].lifeTime--;
		}
		while(iUpdate--);
	}
}

class EmitPoint {
	public var x:int;
	public var y:int;
	
	public function EmitPoint(x:int = 0, y:int = 0)
	{
		this.x = x;
		this.y = y;
	}
}

import flash.text.TextField;
import flash.events.Event;
class Console extends TextField
{
	public function Console()
	{
		text = '';
		selectable = false;
		textColor = 0xFFFFFF;
		addEventListener(Event.ADDED_TO_STAGE, function(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, arguments.callee);
			width = stage.stageWidth;
			height = stage.stageHeight;
			
		});
	}
	
	public function log(message:String):void
	{
		this.appendText(message + '\n');
	}
	
	public function clear():void{
		this.text = '';
	}
	
}