Last Updated: February 25, 2016
·
1.557K
· burakkirkil

Tween 3D Helper AS3 Class

package tr.com.gotoandrock.utils
{
    import com.greensock.TweenLite;

    import flash.display.DisplayObject;
    import flash.geom.Matrix;

    /**
     * Helps 3d tranformation applied target back to normal 2D Matrix and look&feel :)
     * Should only use tween ends with if z value equal 0;
     * Attention Please: onComplete and onCompleteParams overrides
     * @author Burak Kırkıl
     * 
     */ 
    public class Tween3D
    {
        public static function to(target:DisplayObject, duration:Number, vars:Object):void
        {
            TweenLite.to(target, duration, scaleTargetAndGet2dVars(target, vars));
        }

        public static function from(target:DisplayObject, duration:Number, vars:Object):void
        {
            TweenLite.from(target, duration, scaleTargetAndGet2dVars(target, vars));
        }

        //////////////////////////////////////////////////////
        // PRIVATE FUNCS
        //////////////////////////////////////////////////////
        private static function scaleTargetAndGet2dVars(target:DisplayObject, vars:Object):Object
        {
            // create vars
            vars.onComplete = backTo2DTarget;
            vars.onCompleteParams = [target, target.transform.matrix];
            // scale target
            target.scaleX = target.width / (target.width + 1);
            target.scaleY = target.height / (target.height + 1);
            return vars;
        }

        private static function backTo2DTarget(target:DisplayObject, matrix2d:Matrix):void
        {
            // normalize matrix
            target.transform.matrix = matrix2d;
            target.transform.matrix3D = null;
        }
    }
}