Matrix Math Practice in AS3.0 with TweenMax & transformMatrix Plugin

package
{
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import com.greensock.TweenMax;
	import com.greensock.plugins.*;
	
	[SWF (width="500", height="500", backgroundColor="#CCCCCC", frameRate="30")]		
	public class MatrixMathTesting extends Sprite
	{
		private var mc:MovieClip = new MovieClip();
		private var wishMatrix:Matrix = new Matrix;
		
		public function MatrixMathTesting()
		{
			TweenPlugin.activate([TransformMatrixPlugin]);
			
			mc.x = 250;
			mc.y = 250;
			addChild( mc );
			
			var sq:Shape = new Shape();
			sq.graphics.beginFill( 0x000000, .5 );
			sq.graphics.drawRect( 0, 0, 100, 100 );
			sq.graphics.endFill();			
			
			mc.addChild( sq );			
			
			var ar:Array = [ [2,   0], 		//[x-scale (a), y-skew (b)]
					  		 [0.5, 1],		//[ x-skew (c), y-scale (d) ]
					   		 [mc.x, mc.y] ]; //[x-position (tx), y-position (ty)] 
			
			transformMC( wishMatrix, ar );
			
			TweenMax.delayedCall( 2, removeTransform, [wishMatrix] );
			 
		}
	
		public function transformMC( m:Matrix, arr:Array ):void 
		{
			TweenMax.to(mc, 1, {transformMatrix:{ a:arr[0][0],  b:arr[0][1], 
										          c:arr[1][0],  d:arr[1][1], 
												  tx:arr[2][0], ty:arr[2][1] }});						
		}
		
		public function transformMCSimple( m:Matrix, arr:Array ):void 
		{			
			m.a  = arr[0][0];  //	 x-scale
			m.b  = arr[0][1];  //  	 y-skew
			m.c  = arr[1][0];  //	 x-skew 
			m.d  = arr[1][1];  //		 y-scale
			m.tx = arr[2][0]; //	 x-position 
			m.ty = arr[2][1]; //	 y-position 
			
			mc.transform.matrix = wishMatrix;
		}
		
		public function removeTransform( m:Matrix ):void
		{		
			//tween back to an identity matrix		
			TweenMax.to(mc, 1, {transformMatrix:{a:1, b:0, c:0, d:1, tx:mc.x, ty:mc.y }}); 
		}
		
		public function removeTransformSimple( m:Matrix ):void
		{		
			//no anmation, back to identity matrix (removes all previous transformations)
			m.a  = 1; m.b  = 0; 
			m.c  = 0; m.d  = 1;  
			m.tx = m.tx; m.ty = m.ty;			
			
			mc.transform.matrix = m;			
		}		
		
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s