00001 import mx.utils.Delegate;
00002
00003 import gugga.common.ITask;
00004 import gugga.debug.Assertion;
00005 import gugga.events.EventDispatcher;
00006 import gugga.utils.OnEnterFrameBeacon;
00007
00016 class gugga.animations.FrameByFrameAnimation
00017 extends EventDispatcher implements ITask
00018 {
00019 private var mMovieClip : MovieClip;
00020 private var mStartFrame : Number;
00021 private var mTargetFrame : Number;
00022
00023 private var mStartFromCurrentFrame : Boolean = false;
00024
00025 private var mIsRunning : Boolean = false;
00026
00027 private var onEnterFrameDelegate : Function;
00028
00032 public function FrameByFrameAnimation (aMovieClip : MovieClip, aEnd : Object, aStart : Object)
00033 {
00034 super();
00035
00036 mMovieClip = aMovieClip;
00037
00038 var currentFrame : Number = mMovieClip._currentframe;
00039
00040 if(aStart == undefined)
00041 {
00042 mStartFrame = mMovieClip._currentframe;
00043 mStartFromCurrentFrame = true;
00044 }
00045 else if(!isNaN(Number(aStart)))
00046 {
00047 mStartFrame = Number(aStart);
00048 }
00049 else
00050 {
00051 mMovieClip.gotoAndStop(String(aStart));
00052 mStartFrame = mMovieClip._currentframe;
00053 mMovieClip.gotoAndStop(currentFrame);
00054 }
00055
00056 if(!isNaN(Number(aEnd)))
00057 {
00058 mTargetFrame = Number(aEnd);
00059 }
00060 else
00061 {
00062 mMovieClip.gotoAndStop(String(aEnd));
00063 mTargetFrame = mMovieClip._currentframe;
00064 mMovieClip.gotoAndStop(currentFrame);
00065 }
00066
00067 Assertion.warningIfFalse(
00068 mMovieClip._totalframes >= mTargetFrame && mMovieClip._totalframes >= mStartFrame && mStartFrame > 0 && mTargetFrame > 0,
00069 "Invalid arguments.",
00070 this, arguments);
00071
00072 onEnterFrameDelegate = Delegate.create(this, onEnterFrameHandler);
00073 }
00074
00075 public function start() : Void
00076 {
00077 mIsRunning = true;
00078 OnEnterFrameBeacon.Instance.addEventListener("onEnterFrame", onEnterFrameDelegate);
00079
00080 if(mStartFromCurrentFrame)
00081 {
00082 mStartFrame = mMovieClip._currentframe;
00083 }
00084
00085 mMovieClip.gotoAndStop(mStartFrame);
00086 onEnterFrameHandler();
00087 }
00088
00089 public function isRunning() : Boolean
00090 {
00091 return mIsRunning;
00092 }
00093
00094 public function isImmediatelyInterruptable() : Boolean
00095 {
00096 return true;
00097 }
00098
00099 public function interrupt() : Void
00100 {
00101 OnEnterFrameBeacon.Instance.removeEventListener("onEnterFrame", onEnterFrameDelegate);
00102 mIsRunning = false;
00103 dispatchEvent({type:"interrupted", target:this});
00104 }
00105
00106 private function onEnterFrameHandler () : Void
00107 {
00108 if(mMovieClip._currentframe < mTargetFrame)
00109 {
00110 mMovieClip.nextFrame();
00111 }
00112 else if(mMovieClip._currentframe > mTargetFrame)
00113 {
00114 mMovieClip.prevFrame();
00115 }
00116
00117 if(mMovieClip._currentframe == mTargetFrame)
00118 {
00119 OnEnterFrameBeacon.Instance.removeEventListener("onEnterFrame", onEnterFrameDelegate);
00120 mIsRunning = false;
00121 dispatchEvent({type:"completed", target:this});
00122 }
00123 }
00124 }