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

改 : Particle と Attractor の練習

[改:パーティクルとアトラクタの練習]
* 前回のものより処理が少しだけ早くなりました。
* もっと処理が軽くならないか試行錯誤中です。
* http://wonderfl.net/code/c35d974910ef4090e975817d494e96d08edea5c9
*  
* 配列のシャッフルに関しては下記を参考にさせていただきました。
* http://www.project-nya.jp/modules/weblog/details.php?blog_id=1046
*
// forked from enoeno's Particle と Attractor の練習
/*
* [改:パーティクルとアトラクタの練習]
* 前回のものより処理が少しだけ早くなりました。
* もっと処理が軽くならないか試行錯誤中です。
* http://wonderfl.net/code/c35d974910ef4090e975817d494e96d08edea5c9
*  
* 配列のシャッフルに関しては下記を参考にさせていただきました。
* http://www.project-nya.jp/modules/weblog/details.php?blog_id=1046
* 
*/

package {
	
	import flash.display.*;
	import flash.geom.*;
	import flash.events.*;
	
	import net.hires.debug.Stats;
	
	public class Main extends MovieClip {
		
		private var WIDTH:Number;
		private var HEIGHT:Number;
		
		private var _particle:Vector.<Particle>;
		private var _attractor:Vector.<Attractor>;
		private var _posX:Vector.<Number>;
		private var _posY:Vector.<Number>;
		private var _partsAry:Vector.<Shape>;
		
		/*
		private var _particle:Array;
		private var _attractor:Array;
		private var _posX:Array;
		private var _posY:Array;
		private var _partsAry:Array;
		*/
		
		private const NUM_PARTICLE:uint = 3500;
		private const NUM_ATTRACTOR:uint = 8;
		
		public var damp:Number = 0.00002;
		public var accel:Number = 10000;
		
		private var _screen:BitmapData;
		private var _parts:Shape;
		
		
		private var _palette:Array;
		private var _colR:Number, _colG:Number, _colB:Number;
		private var _color:ColorTransform;
		
		public function Main():void
		{
			init();
		}
		
		private function init():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			WIDTH = stage.stageWidth;
			HEIGHT = stage.stageHeight;
			
			_screen = new BitmapData( WIDTH, HEIGHT, false, 0x0 );
			addChild( new Bitmap(_screen) ) as Bitmap;
		
			stage.addEventListener( Event.ENTER_FRAME, update );
			stage.addEventListener( MouseEvent.CLICK, reset );
			stage.addEventListener( Event.RESIZE, resize );
			//addChild(new Stats());
			Wonderfl.capture_delay(100);
			reset();
		}
		
		private function reset( e:Event = null ):void
		{
			_particle = new Vector.<Particle>(NUM_PARTICLE, true);
			_attractor = new Vector.<Attractor>(NUM_ATTRACTOR, true);
			_posX = new Vector.<Number>(NUM_PARTICLE, true);
			_posY = new Vector.<Number>(NUM_PARTICLE, true);
			_partsAry = new Vector.<Shape>(NUM_PARTICLE, true);
			
			/*
			_particle = [];
			_attractor = [];
			_posX = [];
			_posY = [];
			_partsAry = [];
			*/
			

			var col1:int, col2:int, col3:int;
			col1 = Math.floor( Math.random()*128 + 128 );
			col2 = Math.floor( Math.random()*( col1 * 0.5 ) + col1 * 0.105 );
			col3 = Math.floor( Math.random()*( col2 * 0.5 ) + col2 * 0.105 );
			_palette = [col1, col2, col3];
			_palette = shuffle(_palette);
			
			_colR = _palette[0];
			_colG = _palette[1];
			_colB = _palette[2];
			
			_color = new ColorTransform(0, 0, 0, 0.4, _colR, _colG, _colB, 0);
			
			
			_screen.fillRect( _screen.rect, 0x0 );
			
			var i:uint;
			
			// init Attractors
			for ( i = 0; i < NUM_ATTRACTOR; i++) {
				
				var a:Attractor = new Attractor();
				a.x = Math.random()*WIDTH;
				a.y = Math.random()*HEIGHT;
				
				_attractor[i] = a;
			}
			
			//init Particles
			for (i = 0; i < NUM_PARTICLE; i++) {
				var p:Particle = new Particle();
				
				p.x = Math.random()*WIDTH;
				p.y = Math.random()*HEIGHT;
				
				p.vx = -accel/2 + Math.random()*accel;
				p.vy = -accel/2 + Math.random()*accel;
				
				_particle[i] = p;
				
				
				_parts = new Shape();
				_parts.graphics.beginFill( 0xFFFFFF, 1 );
				_parts.graphics.drawCircle( 0, 0, 0.2 );
				_parts.blendMode = BlendMode.ADD;
				_partsAry[i] = _parts;
			}
		}
		
		private function resize( e:Event = null ):void
		{
			stage.removeEventListener( Event.ENTER_FRAME, update );
			stage.removeEventListener( MouseEvent.CLICK, reset );
			stage.removeEventListener( Event.RESIZE, resize );
			init();
		}
		
		
		private function update( e:Event=null ):void
		{
			updatePos();
			
		}
		
		private function updatePos():void
		{
			var p:Particle;
			var a:Attractor;
			var d:Number;
			
			
			for( var i:uint = 0; i < NUM_PARTICLE; i++ ){
				
				p = _particle[i];
				
				for( var k:uint = 0; k < NUM_ATTRACTOR; k++ ){
				  
				  a = _attractor[k];
				  d = Math.pow(a.x-p.x, 2) + Math.pow(a.y-p.y, 2);
				  
				  if (d > 0.1) {
					p.vx += accel * (a.x-p.x) / d;
					p.vy += accel * (a.y-p.y) / d;
				  }
				}
				
				p.x += p.vx;
				p.y += p.vy;
				p.vx *= damp;
				p.vy *= damp;
				
				_posX[i] = p.x;
				_posY[i] = p.y;
			}
			draw();
		}
		
		private function draw():void
		{
			var canvas:Sprite = new Sprite();
			var parts:Shape;
			
			_screen.lock();
			
			for( var i:uint = 0; i < NUM_PARTICLE; i++ )
			{
				parts = _partsAry[i];
				parts.x = _posX[i];
				parts.y = _posY[i];
				
				canvas.addChild( parts );
			}
			_screen.draw( canvas, null, _color, BlendMode.ADD, null, false );
			canvas = null;
			
			_screen.unlock();
		}
		
		public function shuffle(list:Array):Array
		{
            var n:uint = list.length;
            var sList:Array = list.concat();
            while (n--) {
                var i:uint = uint(Math.random()*(n+1));
                var t:Object = sList[n];
                sList[n] = sList[i];
                sList[i] = t;
            }
            return sList;
        }
		
	}
}


class Particle {
	public var x:Number;
	public var y:Number;
	public var vx:Number;
	public var vy:Number;
	
	public function Particle()
	{
		x = 0;
		y = 0;
		vx = 0;
		vy = 0;
	}
}

class Attractor
{
	public var x:Number;
	public var y:Number;
	
	public function Attractor()
	{
		x = 0;y = 0;
	}
}