PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/src/org/asaplibrary/util/FrameDelay.as

http://asaplibrary.googlecode.com/
ActionScript | 90 lines | 34 code | 4 blank | 52 comment | 7 complexity | 459aa9c2d34cbf1cb8386d4dfe1ad9a9 MD5 | raw file
 1/*
 2Copyright 2007-2011 by the authors of asaplibrary, http://asaplibrary.org
 3Copyright 2005-2007 by the authors of asapframework, http://asapframework.org
 4
 5Licensed under the Apache License, Version 2.0 (the "License");
 6you may not use this file except in compliance with the License.
 7You may obtain a copy of the License at
 8
 9   	http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16 */
17package org.asaplibrary.util {
18	import flash.events.Event;
19
20	/**
21	Delay a function call with one or more frames. Use this when initializing a SWF or a bunch of DisplayObjects, to enable the player to do its thing.	Usually a single frame delay will do the job, since the next enterFrame will come when all other jobs are finished.
22	@usage
23	To execute function 'init' after 1 frame, use:
24	<code>
25	new FrameDelay(init);
26	</code>
27	To execute function 'init' after 10 frames, use:
28	<code>
29	new FrameDelay(init, 10);
30	</code>
31	To call function 'setProps' with parameters, executed after 1 frame:
32	<code>
33	new FrameDelay(setProps, 1, [shape, 'alpha', 0]);
34	</code>
35	<code>
36	private function setProps (inShape:Shape, inProperty:String, inValue:Number) : void {
37	inShape[inProperty] == inValue);
38	}
39	</code>
40	 */
41	public class FrameDelay {
42		private var mIsDone : Boolean = false;
43		private var mCurrentFrame : int;
44		private var mCallback : Function;
45		private var mParams : Array;
46
47		/**
48		Creates a new FrameDelay. Starts the delay immediately.
49		@param inCallback: the callback function to be called when done waiting
50		@param inFrameCount: the number of frames to wait; when left out, or set to 1 or 0, one frame is waited
51		@param inParams: list of parameters to pass to the callback function
52		 */
53		public function FrameDelay(inCallback : Function, inFrameCount : int = 0, inParams : Array = null) {
54			mCurrentFrame = inFrameCount;
55			mCallback = inCallback;
56			mParams = inParams;
57			mIsDone = (isNaN(inFrameCount) || (inFrameCount <= 1));
58			FramePulse.addEnterFrameListener(handleEnterFrame);
59		}
60
61		/**
62		Release reference to creating object.
63		Use this to remove a FrameDelay object that is still running when the creating object will be removed.
64		 */
65		public function die() : void {
66			if (!mIsDone) {
67				FramePulse.removeEnterFrameListener(handleEnterFrame);
68			}
69		}
70
71		/**
72		Handle the onEnterFrame event.
73		Checks if still waiting - when true: calls callback function.
74		@param e: not used
75		 */
76		private function handleEnterFrame(e : Event) : void {
77			if (mIsDone) {
78				FramePulse.removeEnterFrameListener(handleEnterFrame);
79				if (mParams == null) {
80					mCallback();
81				} else {
82					mCallback.apply(null, mParams);
83				}
84			} else {
85				mCurrentFrame--;
86				mIsDone = (mCurrentFrame <= 1);
87			}
88		}
89	}
90}