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

マウスの軌跡にパーティクルを出す

//////////////////////////////////////////////////////////////////////////////
マウスの軌跡にパーティクルを出す
//////////////////////////////////////////////////////////////////////////////
Get Adobe Flash player
by twistcube 21 Apr 2009
////////////////////////////////////////////////////////////////////////////////
//
//  マウスの軌跡にパーティクルを出す
//
////////////////////////////////////////////////////////////////////////////////
package
{
	import flash.display.* ;
	import flash.events.* ;
	import flash.net.* ;
	import flash.system.* ;
	
	[SWF(width="500", height="500", backgroundColor="0xAD9F6E", frameRate="30")]
	public class Stardust extends Sprite
	{
		private var _distance:Object  = { min:200, max:300 } ;   // 落ちる距離
		private var _move_spd:Object  = { min:0.01, max:0.03 } ; // 落ちる速さ
		private var _clear_spd:Object = { min:0.02, max:0.06 } ; // 消える速さ
		private var _scale_spd:Object = { min:0.02, max:0.06 } ; // 拡大/縮小する速さ
		
		private var _rotation:Number = 0 ;
		private var _prevX:Number = 0 ;
		private var _prevY:Number = 0 ;
		private var _renderCount:uint = 0 ;
		
		public function Stardust( )
		{
			init( ) ;
		}
		
		private function init( ):void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE ;
			stage.align = StageAlign.TOP_LEFT ;
			// stage.quality = StageQuality.LOW ;
			
			addEventListener( Event.ENTER_FRAME, onRender );
		}
		
		public function xRotation( ):Number
		{
			return _rotation ;
		}
		
		private function onRender( event:Event ):void
		{
			if ( _prevX == mouseX && _prevY == mouseY )
			{
				return ;
			}
			
			_rotation = Math.atan2( _prevX - mouseX, _prevY - mouseY ) * 180 / Math.PI + 90 ;
			_prevX = mouseX ;
			_prevY = mouseY ;
			
			for ( var i:Number=0; i<10; i++ )
			{
				// パーティクルを生成
				var obj:StarSprite = new StarSprite( _distance, _move_spd, _clear_spd, _scale_spd, _rotation ) ;
				obj.x = mouseX ;
				obj.y = mouseY ;
				obj.addEventListener( Event.COMPLETE, onComplete ) ;
				
				addChild( obj ) ;
				
				// 4個に1個だけ大きくする
				if ( _renderCount == 4 )
				{
					obj.scaleX = obj.scaleY = Math.random( ) * 2 + 0.8 ;
					_renderCount = 0 ;
				}
				else
				{
					obj.scaleX = obj.scaleY = Math.random( ) * .4 + .4 ;
					_renderCount++ ;
				}
			}
		}
		
		private function onComplete( event:Event ):void
		{
			var obj:StarSprite = event.target as StarSprite ;
			removeChild( obj ) ;
			obj = null ;
		}
	}
}


import flash.display.* ;
import flash.events.* ;
import flash.filters.* ;

class StarSprite extends Sprite
{
	private var _distance:Number ;  // 落ちる距離
	private var _move_spd:Number ;  // 落ちる速さ
	private var _clear_spd:Number ; // 消える速さ
	private var _scale_spd:Number ; // 拡大/縮小する速さ
	private var _rotation:Number ;
	private var _vx:Number ;
	private var _vy:Number ;

	public function StarSprite( _distance:Object, _move_spd:Object, _clear_spd:Object, _scale_spd:Object, _rotation:Number ):void
	{
		this._distance  = _distance.min  + Math.random( ) * ( _distance.max - _distance.min ) ;
		this._move_spd  = _move_spd.min  + Math.random( ) * ( _move_spd.max - _move_spd.min ) ;
		this._clear_spd = _clear_spd.min + Math.random( ) * ( _clear_spd.max - _clear_spd.min ) ;
		this._scale_spd = _scale_spd.min + Math.random( ) * ( _scale_spd.max - _scale_spd.min ) ;
		this._rotation  = _rotation ;
		
		addEventListener( Event.ADDED_TO_STAGE, init ) ;
		addEventListener( Event.REMOVED_FROM_STAGE, unload ) ;
	}

	private function init( event:Event ):void
	{
		removeEventListener( Event.ADDED_TO_STAGE, init ) ;
		
		graphics.clear( ) ;
		graphics.beginFill( 0x412e1d );
		graphics.drawCircle( 0, 0, 10 ) ;
		graphics.endFill( ) ;
		
		//filters = [ new DropShadowFilter( 10, 45, 0x7c863d, 0.5, 5, 5, 5 ) ] ;
		
		xAction( ) ;
	}
	
	private function unload( event:Event = null ):void
	{
		removeEventListener( Event.REMOVED_FROM_STAGE, unload ) ;
		removeEventListener( Event.ENTER_FRAME, onRender ) ;
	}
	
	private function xAction( ):void
	{
		var rot:Number   = _rotation + Math.random( ) * 120 - 60 ;
		var theta:Number = rot * Math.PI / 180 ;
		
		_vx = x - Math.cos( theta ) * _distance ;
		_vy = y + Math.sin( theta ) * _distance ;
		
		addEventListener( Event.ENTER_FRAME, onRender ) ;
	}
	
	private function onRender( event:Event ):void
	{
		x += ( _vx - x ) * _move_spd ;
		y += ( _vy - y ) * _move_spd ;
		alpha -= _clear_spd ;
		scaleX += _scale_spd ;
		scaleY += _scale_spd ;
		
		if ( alpha < 0 )
		{
			dispatchEvent( new Event( Event.COMPLETE ) ) ;
		}
	}
}