Rewind and Forward of a Movieclip - AdvancedMovieClip AS3 Class

I was in need of a subclass of MovieClip that could rewind and forward with a simple method call. That is the result. You can specify a callback function to call after the timeline playhead cease moving. It is very raw at the moment, i am still learning. Any suggestion or modification to the code is greatly appreciated.

package
{
	import flash.display.MovieClip;
	import flash.events.Event; 

	public class AdvancedMovieClip extends MovieClip {

		var dir:String = null;
		var _targetframe:Number;
		var _callback:Function = new Function();
		var _params:Object = null;
		var _numberOfFrames:Number; 

		public function AdvancedMovieClip():void {
			super();
			_numberOfFrames = this.totalFrames;
		}

		public function block() {
			removeEventListener(Event.ENTER_FRAME, enterframeHandler);
		}

		public function forward(targetframe:Number = 25, callback:Function = null, params:Object = null):void {
			dir = "forward";
			_callback = callback;
			_params = params;
			_targetframe = (targetframe == 25) ? this.totalFrames : targetframe;
			addEventListener(Event.ENTER_FRAME, enterframeHandler);
		}

		public function backward(targetframe:Number = 1, callback:Function = null, params:Object = null):void {
			dir = "backward";
			_callback = callback;
			_params = params;
			_targetframe = (targetframe == 1) ? 1 : targetframe;
			addEventListener(Event.ENTER_FRAME, enterframeHandler);
		}

		private function enterframeHandler(e:Event):void {
			switch(dir) {
				case "forward":
				((this.currentFrame == this.totalFrames) || (this.currentFrame == _targetframe)) ? (stopMovement()) : this.nextFrame();
				break;
				case "backward":
				((this.currentFrame == 1) || (this.currentFrame == _targetframe)) ? (stopMovement()) : this.prevFrame();
				break;
			}
		}	

		private function stopMovement() {
			removeEventListener(Event.ENTER_FRAME, enterframeHandler);
			if (_callback != null) {
				_callback(_params);
			}
		}

	}

}

Leave a Comment