Genie Effect AS

package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import org.libspark.betweenas3.BetweenAS3;
	import org.libspark.betweenas3.easing.Cubic;
	import org.libspark.betweenas3.tweens.ITween;

	[SWF( frameRate="60" )]
	public class test_genie extends Sprite
	{
		[Embed( source="assets/image2.jpg" )]
		private var img: Class;
		
		private var bitmap: Bitmap;
		private var bitmapData: BitmapData;
		private var points: Vector.<Vector.<Point>>;
		private var segments: int = 30;
		private var sprite: Sprite;
		private var vertex: Vector.<Number>;
		private var indices: Vector.<int>;
		private var uvdata: Vector.<Number>;

		public function test_genie()
		{
			addEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
		}

		private function draw( e: Event = null ): void
		{
			vertex.length = 0;
			indices.length = 0;
			uvdata.length = 0;
			var k: int = 0;
			var tf: TextField;
			var mx: Number = Math.max( 0, Math.min( bitmapData.width, sprite.mouseX ));
			var my: Number = Math.max( 0, Math.min( bitmapData.height, sprite.mouseY ));
			var mouse: Point = new Point( mx, my );
			var maxdist: Number = Math.sqrt(( bitmapData.width * bitmapData.width ) + ( bitmapData.height * bitmapData.height ));
			var center: Point = new Point( bitmapData.width / 2, bitmapData.height / 2 );
			var npoint: Point;
			center = mouse;

			for ( var x: int = 0; x < points.length; x++ )
			{
				var pt: Point;

				for ( var y: int = 0; y < points[x].length; y++ )
				{
					pt = points[x][y].clone();
					var uvx: Number = x / segments;
					var uvy: Number = y / segments;
					vertex.push( pt.x );
					vertex.push( pt.y );

					if ( y < segments )
						indices.push( k, k + segments + 1, k + 1, k + segments + 1, k + segments + 2, k + 1 );
					uvdata.push( uvx, uvy );
					++k;
				}
			}
			sprite.graphics.clear();
			sprite.graphics.beginBitmapFill( bitmapData, null, false, false );
			sprite.graphics.drawTriangles( vertex, indices, uvdata );
			sprite.graphics.endFill();
		}

		private function onAddedToStage( event: Event ): void
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			this.x = this.y = 20;
			bitmap = new img() as Bitmap;
			bitmapData = bitmap.bitmapData;
			addChild( sprite = new Sprite());
			
			test();
		}

		private function onClick(event: Event ):void
		{
			var point: Point;
			var tweens: Array = [];
			var mpoint: Point = new Point( this.mouseX, this.mouseY );
			
			var k: int = 0;
			var delay: Number;
			
			for ( var y: int = segments; y >= 0; y-- )
			{
				for ( var x: int = 0; x < segments + 1; x++ )
				{
					point = points[x][y];
					
					delay = ( k/1000 ) + ( x/1000 );
					
					tweens.push( BetweenAS3.delay( 
						BetweenAS3.tween( 
							points[x][y], { 
								x: mpoint.x,
								y: mpoint.y
							},
							null,
							0.4, org.libspark.betweenas3.easing.Cubic.easeIn ), delay / 2
						) 
					);
					
					++k;
				}
			}
			
			var itw: ITween = BetweenAS3.parallelTweens( tweens );
			itw.onComplete = onTweenComplete;
			itw.play();
		}
		
		private function onTweenComplete(): void
		{
			var t: Timer = new Timer( 500, 1 );
			t.addEventListener( TimerEvent.TIMER_COMPLETE, test, false, 0, true );
			t.start();
		}

		private function test( event: Event = null ): void
		{
			stage.addEventListener( MouseEvent.CLICK, onClick );
			
			points = new Vector.<Vector.<Point>>();
			vertex = new Vector.<Number>();
			indices = new Vector.<int>();
			uvdata = new Vector.<Number>();

			for ( var x: int = 0; x < segments + 1; x++ )
			{
				points[x] = new Vector.<Point>();
				var px: Number = ( x / segments ) * bitmapData.width;

				for ( var y: int = 0; y < segments + 1; y++ )
				{
					var py: Number = ( y / segments ) * bitmapData.height;
					points[x][y] = new Point( px, py );
				}
			}
			draw();
			addEventListener( Event.ENTER_FRAME, draw );
		}
	}
}
 

你可能感兴趣的:(effect)