您的位置:首页 > 服装鞋帽 > 男装 > 源码赏析 tween manager

源码赏析 tween manager

luyued 发布于 2011-05-31 10:24   浏览 N 次  
原文地址:http://www.actionscript.org/resources/articles/1075/1/Create-your-own-tween-manager-class-in-AS3/Page1.html国外老兄的一个AS3源码 。原理很看似简单,通过Sept来计算一个对象的x,y 属性信息,最后通过loop来展现。各个数学模型是亮点,这个集中在Ease.as中,有兴趣的朋友可以参看原网站,这里贴出核心代码,供学习和参考。

tweenmanager.as:
package {import flash.display.MovieClip;import flash.events.Event;public class TweenManager extends MovieClip {private var _objectToModify:Object;private var _properties:Object;public function TweenManager( objectToModify:Object, properties:Object ) {_objectToModify = objectToModify;_properties = properties;}public function start():void {for( var i:* in _properties ) {_properties[i].values = calculateValues( _properties[i].start, _properties[i].end, _properties[i].steps, _properties[i].method );_properties[i].valueIndex = 0;_properties[i].isItDone = false;}addEventListener( Event.ENTER_FRAME, loop );}public function loop( e:Event ):void {// looping through the propertiesfor( var i:* in _properties ) {// checking if the properies twee it's doneif( !_properties[i].isItDone ) {// checking if the current value is the last oneif(_properties[i].valueIndex == _properties[i].steps+1) {// marking the tween as a done_properties[i].isItDone = true;} else {// applying the value_objectToModify[i] = _properties[i].values[_properties[i].valueIndex];// incrementing the values' index_properties[i].valueIndex += 1;}}}// checking if all the tweens are donevar areAllDone:Boolean = true;for (i in _properties) {if (!_properties[i].isItDone) {areAllDone = false;}}// if yes then remove the listenerif (areAllDone) {removeEventListener(Event.ENTER_FRAME, loop);}}private function calculateValues(startPos:Number, endPos:Number, steps:Number, mtd:String):Array {var arr:Array = new Array();var calcEndPos:Number;for ( var i:Number = 0; i <= steps; i++ ){calcEndPos = ( endPos - startPos );switch( mtd ){case "InBack": arr[i] = Ease.InBack(i, startPos, calcEndPos, steps, 1.70158); break;case "OutBack": arr[i] = Ease.OutBack(i,startPos,calcEndPos,steps, 1.70158); break;case "InOutBack": arr[i] = Ease.InOutBack(i,startPos,calcEndPos,steps, 1.70158); break;case "OutBounce": arr[i] = Ease.OutBounce(i,startPos,calcEndPos,steps); break;case "InBounce": arr[i] = Ease.InBounce(i,startPos,calcEndPos,steps); break;case "InOutBounce": arr[i] = Ease.InOutBounce(i,startPos,calcEndPos,steps); break;case "InCirc": arr[i] = Ease.InCirc(i,startPos,calcEndPos,steps); break;case "OutCirc": arr[i] = Ease.OutCirc(i,startPos,calcEndPos,steps); break;case "InOutCirc": arr[i] = Ease.InOutCirc(i,startPos,calcEndPos,steps); break;case "In": arr[i] = Ease.In(i,startPos,calcEndPos,steps); break;case "Out": arr[i] = Ease.Out(i,startPos,calcEndPos,steps); break;case "InOut": arr[i] = Ease.InOut(i,startPos,calcEndPos,steps); break;case "InElastic": arr[i] = Ease.InElastic(i,startPos,calcEndPos,steps, 0, 0); break;case "OutElastic": arr[i] = Ease.OutElastic(i,startPos,calcEndPos,steps, 0, 0); break;case "InOutElastic": arr[i] = Ease.InOutElastic(i,startPos,calcEndPos,steps, 0, 0); break;case "InExpo": arr[i] = Ease.InExpo(i,startPos,calcEndPos,steps); break;case "OutExpo": arr[i] = Ease.OutExpo(i,startPos,calcEndPos,steps); break;case "InOutExpo": arr[i] = Ease.InOutExpo(i,startPos,calcEndPos,steps); break;case "Linear": arr[i] = Ease.Linear(i,startPos,calcEndPos,steps); break;case "InLinear": arr[i] = Ease.InLinear(i,startPos,calcEndPos,steps); break;case "OutLinear": arr[i] = Ease.OutLinear(i,startPos,calcEndPos,steps); break;case "InOutLinear": arr[i] = Ease.InOutLinear(i,startPos,calcEndPos,steps); break;case "InQuad": arr[i] = Ease.InQuad(i,startPos,calcEndPos,steps); break;case "OutQuad": arr[i] = Ease.OutQuad(i,startPos,calcEndPos,steps); break;case "InOutQuad": arr[i] = Ease.InOutQuad(i,startPos,calcEndPos,steps); break;case "InQuart": arr[i] = Ease.InQuart(i,startPos,calcEndPos,steps); break;case "OutQuart": arr[i] = Ease.OutQuart(i,startPos,calcEndPos,steps); break;case "InOutQuart": arr[i] = Ease.InOutQuart(i,startPos,calcEndPos,steps); break;case "InQuint": arr[i] = Ease.InQuint(i,startPos,calcEndPos,steps); break;case "OutQuint": arr[i] = Ease.OutQuint(i,startPos,calcEndPos,steps); break;case "InOutQuint": arr[i] = Ease.InOutQuint(i,startPos,calcEndPos,steps); break;case "InSine": arr[i] = Ease.InQuint(i,startPos,calcEndPos,steps); break;case "OutSine": arr[i] = Ease.OutQuint(i,startPos,calcEndPos,steps); break;case "InOutSine": arr[i] = Ease.InOutQuint(i,startPos,calcEndPos,steps); break;default: arr[i] = Ease.Linear(i,startPos,calcEndPos,steps); break;}}return arr;}}
}ease.as
package {

/*** Author: Robert Penner*/
public class Ease {//Backpublic static function InBack (t:Number, b:Number, c:Number, d:Number, s:Number):Number {//if (s == undefined) s = 1.70158;return c*(t/=d)*t*((s+1)*t - s) + b;}public static function OutBack (t:Number, b:Number, c:Number, d:Number, s:Number):Number {//if (s == undefined) s = 1.70158;return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;}public static function InOutBack (t:Number, b:Number, c:Number, d:Number, s:Number):Number {//if (s == undefined) s = 1.70158; if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;}//Bouncepublic static function OutBounce (t:Number, b:Number, c:Number, d:Number):Number {if ((t/=d) < (1/2.75)) {return c*(7.5625*t*t) + b;} else if (t < (2/2.75)) {return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;} else if (t < (2.5/2.75)) {return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;} else {return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;}}public static function InBounce(t:Number, b:Number, c:Number, d:Number):Number {return c - OutBounce (d-t, 0, c, d) + b;}public static function InOutBounce (t:Number, b:Number, c:Number, d:Number):Number {if (t < d/2) return InBounce (t*2, 0, c, d) * .5 + b;else return OutBounce (t*2-d, 0, c, d) * .5 + c*.5 + b;}//Circpublic static function InCirc (t:Number, b:Number, c:Number, d:Number):Number {return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;}public static function OutCirc (t:Number, b:Number, c:Number, d:Number):Number {return c * Math.sqrt(1 - (t=t/d-1)*t) + b;}public static function InOutCirc (t:Number, b:Number, c:Number, d:Number):Number {if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;}//Cubicpublic static function In (t:Number, b:Number, c:Number, d:Number):Number {return c*(t/=d)*t*t + b;}public static function Out (t:Number, b:Number, c:Number, d:Number):Number {return c*((t=t/d-1)*t*t + 1) + b;}public static function InOut (t:Number, b:Number, c:Number, d:Number):Number {if ((t/=d/2) < 1) return c/2*t*t*t + b;return c/2*((t-=2)*t*t + 2) + b;}//Elasticpublic static function InElastic (t:Number, b:Number, c:Number, d:Number, a:Number, p:Number):Number {if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;if (!a || a < Math.abs(c)) { a=c; var s:Number =p/4; }else s = p/(2*Math.PI) * Math.asin (c/a);return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;}public static function OutElastic (t:Number, b:Number, c:Number, d:Number, a:Number, p:Number):Number {if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;if (!a || a < Math.abs(c)) { a=c; var s:Number = p/4; }else s = p/(2*Math.PI) * Math.asin (c/a);return (a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b);}public static function InOutElastic (t:Number, b:Number, c:Number, d:Number, a:Number, p:Number):Number {if (t==0) return b; if ((t/=d/2)==2) return b+c; if (!p) p=d*(.3*1.5);if (!a || a < Math.abs(c)) { a=c; var s:Number = p/4; }else s = p/(2*Math.PI) * Math.asin (c/a);if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;}//Expopublic static function InExpo (t:Number, b:Number, c:Number, d:Number):Number {return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;}public static function OutExpo (t:Number, b:Number, c:Number, d:Number):Number {return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;}public static function InOutExpo (t:Number, b:Number, c:Number, d:Number):Number {if (t==0) return b;if (t==d) return b+c;if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;}
上一篇:诚招恒河鳄休闲男鞋代理 下一篇:pace
广告赞助商