PageRenderTime 0ms CodeModel.GetById 65ms app.highlight 1ms RepoModel.GetById 75ms app.codeStats 0ms

/DiegoRay/actionscripts/org/asapframework/util/FrameDelay.as

https://github.com/joemaffia/flash-junk
ActionScript | 125 lines | 36 code | 14 blank | 75 comment | 5 complexity | 7fe364dd6492bf21cb3ebdfad7777956 MD5 | raw file
  1/*
  2Copyright 2005-2006 by the authors of asapframework, http://asapframework.org
  3
  4Licensed under the Apache License, Version 2.0 (the "License");
  5you may not use this file except in compliance with the License.
  6You may obtain a copy of the License at
  7
  8   	http://www.apache.org/licenses/LICENSE-2.0
  9
 10Unless required by applicable law or agreed to in writing, software
 11distributed under the License is distributed on an "AS IS" BASIS,
 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13See the License for the specific language governing permissions and
 14limitations under the License.
 15*/
 16
 17// ASAP classes
 18import org.asapframework.events.EventDelegate;
 19import org.asapframework.util.framepulse.FramePulse;
 20
 21/**
 22Class that provides one or more frames delay. 
 23Use this when initializing a swf or a bunch of movieclips, to enable the player to do its thing.
 24Usually a single frame delay will do the job, since the next enterFrame will come when all other jobs are finished.
 25This class will catch that next onEnterFrame and fire the function in the object passed as parameters.
 26
 27A parameter is available if the delay has to be more than one frame.
 28
 29@usage
 30<code>
 31	class myClass {
 32	
 33	private var mFrameDelay:FrameDelay;
 34	
 35		function init () : Void {
 36			 ... do a bunch of inits
 37			
 38			// wait one enterFrame
 39			mFrameDelay = new FrameDelay(this, initDone);
 40		}
 41
 42		private function initDone () : Void {
 43			...
 44		}
 45		
 46		private function goAway () {
 47			mFrameDelay.die();
 48	}
 49</code>
 50
 51When starting a swf:
 52
 53<code>
 54	var lc:LocalController = new MyLocalController(this);
 55	// wait one enterFrame before notifying the MovieManager that this movie is done initializing
 56	var fd:FrameDelay = new FrameDelay(lc, lc.notifyMovieInitialized);
 57</code>
 58*/
 59
 60class org.asapframework.util.FrameDelay {
 61	
 62	private var mIsDone:Boolean = false;
 63	private var mCurrentFrame:Number;
 64	private var mSender:Object;
 65	private var mCallback:Function;
 66	private var mParams:Array;
 67	private var mFramePulseListener:Function;
 68
 69	/**
 70	Constructor; starts the waiting immediately.
 71	@param inSender:Object, the class that contains the function to be called when done waiting
 72	@param inCallback:Function, the callback function to be called when done waiting
 73	@param inParams:Array, list of paramters to pass to callback function
 74	@param inFrameCount:Number, the number of frames to wait; when left out, or set to 1 or 0, one frame is waited
 75	*/
 76	public function FrameDelay (inSender:Object, inCallback:Function, inFrameCount:Number, inParams:Array) {		
 77
 78		// create handler for enterFrame events
 79		mFramePulseListener = EventDelegate.create(this, onEnterFrame);
 80	
 81		wait(inSender, inCallback, inFrameCount, inParams);
 82	}
 83
 84	/**
 85	*	Release reference to creating object
 86	*	Use this to remove a FrameDelay object that is still running when the creating object will be removed
 87	*/
 88	public function die () : Void {
 89		if (!mIsDone) {
 90			FramePulse.removeEnterFrameListener(mFramePulseListener);
 91		}
 92	}
 93
 94	// PRIVATE METHODS
 95	
 96	/**
 97	Stores input parameters (see {@link #FrameDelay constructor} parameters), start waiting.
 98	*/
 99	private function wait (inSender:Object, inCallback:Function, inFrameCount:Number, inParams:Array) : Void {
100		mCurrentFrame = inFrameCount;
101		mSender = inSender;
102		mCallback = inCallback;
103		mParams = inParams;
104
105		mIsDone = ((inFrameCount == undefined) || (inFrameCount <= 1));
106
107		// listen to framepulse events
108		FramePulse.addEnterFrameListener(mFramePulseListener);
109	}
110
111	/**
112	Handle the onEnterFrame event.
113	Checks if still waiting - when true: calls callback function.
114	*/
115	private function onEnterFrame () : Void {
116		
117		if (mIsDone) {
118			FramePulse.removeEnterFrameListener(mFramePulseListener);
119			mCallback.apply(mSender,mParams);
120		} else {
121			mCurrentFrame--;
122			mIsDone = (mCurrentFrame <= 1);
123		}
124	}
125}