Progression裡跟Tween有關的Command是結合Tweener的DoTweener,那如果習慣使用TweenMax的話,就可以來動手自訂Command。之前奶綠有一篇有提到Progression 3自訂Command,剛好也是用TweenMax做例子。這邊來實作Progression 4版的。而TweenMax裡有TweenMax.to()、TweenMax.from()、TweenMax.fromTo()等等方法,我們分別來做成DoTweenMaxTo、DoTweenMaxFrom、DoTweenMaxFromTo等3個Command。
DoTweenMaxTo直接列出程式碼,說明就直接寫在註解裡了。
package grayliao.commands { import com.greensock.events.TweenEvent; import com.greensock.TweenMax; import jp.progression.commands.Command; //繼承Command類別 public class DoTweenMaxTo extends Command { private var _tweenMax:TweenMax; private var _target:Object; private var _duration:Number; private var _vars:Object; private var _initObject:Object; //建構式前三個參數就是TweenMax.to要用到的,最後一個initObject保留下來可以輸入Command要的初始值 public function DoTweenMaxTo( target:Object, duration:Number, vars:Object, initObject:Object = null ) { //呼叫父類別的建構式,把_execute,_interrupt,兩個Function,以及initObject傳過去 //_execute與_interrupt後面會實作出來 super( _execute, _interrupt, initObject ); //把輸入進來的參數存下來,後面會用到 _target = target; _duration = duration; _vars = vars; _initObject = initObject; } //執行Command時會呼叫的Function private function _execute():void { //執行TweenMax.to,並把回傳的TweenMax存下來 _tweenMax = TweenMax.to(_target, _duration, _vars); //偵聽TweenEvent.COMPLETE,才知道命令何時結束 _tweenMax.addEventListener(TweenEvent.COMPLETE, _tweenComplete); } //TweenMax結束時的處理函數 private function _tweenComplete(e:TweenEvent):void { //取消偵聽,然後把能清的都清光 _tweenMax.removeEventListener(TweenEvent.COMPLETE, _tweenComplete); _tweenMax = null; _target = null; _vars = null; _initObject = null; //呼叫executeComplete(),Progression就知道這個Command結束了 executeComplete(); } //中斷Command時會呼叫的Function private function _interrupt():void { //取消偵聽,殺掉TweenMax,把能清的都清光 _tweenMax.removeEventListener(TweenEvent.COMPLETE, _tweenComplete); _tweenMax.kill(); _tweenMax = null; _target = null; _vars = null; _initObject = null; } //覆寫clone方法 override public function clone():Command { //把之前存下來的參數都丟進去 return new DoTweenMaxTo( _target, _duration, _vars, _initObject ); } } }
DoTweenMaxFrom與DoTweenMaxFromTo作法類似,就不寫了,請直接下載原始檔參考。
原始檔下載
使用時可以搬到自己常用的package裡去整合起來,還有要記得下載最新版的TweenMax。
有興趣還可以自己動手做做TweenMax.allFrom()、TweenMax.allFromTo()、TweenMax.to()等等Command。
沒有留言:
張貼留言