PageRenderTime 108ms CodeModel.GetById 69ms app.highlight 2ms RepoModel.GetById 35ms app.codeStats 1ms

/lib/temple/utils/FrameDelay.as

http://github.com/MediaMonks/Temple
ActionScript | 152 lines | 47 code | 11 blank | 94 comment | 6 complexity | 8d693d55fdb83f8b2ee6badc37f0aac6 MD5 | raw file
  1/*
  2 *	 
  3 *	Temple Library for ActionScript 3.0
  4 *	Copyright Š 2010 MediaMonks B.V.
  5 *	All rights reserved.
  6 *	
  7 *	http://code.google.com/p/templelibrary/
  8 *	
  9 *	Redistribution and use in source and binary forms, with or without
 10 *	modification, are permitted provided that the following conditions are met:
 11 *	
 12 *	- Redistributions of source code must retain the above copyright notice,
 13 *	this list of conditions and the following disclaimer.
 14 *	
 15 *	- Redistributions in binary form must reproduce the above copyright notice,
 16 *	this list of conditions and the following disclaimer in the documentation
 17 *	and/or other materials provided with the distribution.
 18 *	
 19 *	- Neither the name of the Temple Library nor the names of its contributors
 20 *	may be used to endorse or promote products derived from this software
 21 *	without specific prior written permission.
 22 *	
 23 *	
 24 *	Temple Library is free software: you can redistribute it and/or modify
 25 *	it under the terms of the GNU Lesser General Public License as published by
 26 *	the Free Software Foundation, either version 3 of the License, or
 27 *	(at your option) any later version.
 28 *	
 29 *	Temple Library is distributed in the hope that it will be useful,
 30 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 31 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 32 *	GNU Lesser General Public License for more details.
 33 *	
 34 *	You should have received a copy of the GNU Lesser General Public License
 35 *	along with Temple Library.  If not, see <http://www.gnu.org/licenses/>.
 36 *	
 37 */
 38
 39
 40/*
 41Copyright 2007 by the authors of asaplibrary, http://asaplibrary.org
 42Copyright 2005-2007 by the authors of asapframework, http://asapframework.org
 43
 44Licensed under the Apache License, Version 2.0 (the "License");
 45you may not use this file except in compliance with the License.
 46You may obtain a copy of the License at
 47
 48http://www.apache.org/licenses/LICENSE-2.0
 49
 50Unless required by applicable law or agreed to in writing, software
 51distributed under the License is distributed on an "AS IS" BASIS,
 52WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 53See the License for the specific language governing permissions and
 54limitations under the License.
 55 */
 56
 57package temple.utils 
 58{
 59	import temple.core.CoreObject;
 60
 61	import flash.events.Event;
 62
 63	/**
 64	 * 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.
 65	 * To execute function 'init' after 1 frame, use:
 66	 * 
 67	 * @example
 68	 * <listing version="3.0">
 69	 * new FrameDelay(init);
 70	 * </listing>
 71	 * 
 72	 * To execute function 'init' after 10 frames, use:
 73	 * <listing version="3.0">
 74	 * new FrameDelay(init, 10);
 75	 * </listing>
 76	 * 
 77	 * To call function 'setProps' with parameters, executed after 1 frame:
 78	 * <listing version="3.0">
 79	 * new FrameDelay(setProps, 1, [shape, 'alpha', 0]);
 80	 * </listing>
 81	 * 
 82	 * <listing version="3.0">
 83	 * private function setProps (shape:Shape, property:String, value:Number):void
 84	 * {
 85	 * 		shape[property] = value;
 86	 * }
 87	 * </listing>
 88	 */
 89	public final class FrameDelay extends CoreObject
 90	{
 91		private var _isDone:Boolean = false;
 92		private var _currentFrame:int;
 93		private var _callback:Function;
 94		private var _params:Array;
 95
 96		/**
 97		 * Creates a new FrameDelay. Starts the delay immediately.
 98		 * @param callback the callback function to be called when done waiting
 99		 * @param frameCount the number of frames to wait; when left out, or set to 1 or 0, one frame is waited
100		 * @param params list of parameters to pass to the callback function
101		 */
102		public function FrameDelay(callback:Function, frameCount:int = 1, params:Array = null) 
103		{
104			this._currentFrame = frameCount;
105			this._callback = callback;
106			this._params = params;
107			this._isDone = (isNaN(frameCount) || (frameCount <= 1));
108			FramePulse.addEnterFrameListener(handleEnterFrame);
109		}
110
111
112		/**
113		 * Handle the Event.ENTER_FRAME event.
114		 * Checks if still waiting - when true: calls callback function.
115		 * @param event not used
116		 */
117		private function handleEnterFrame(event:Event):void 
118		{
119			if (this._isDone) 
120			{
121				FramePulse.removeEnterFrameListener(this.handleEnterFrame);
122				if (this._params == null) 
123				{
124					this._callback();
125				}
126				else 
127				{
128					this._callback.apply(null, this._params);
129				}
130			}
131			else 
132			{
133				this._currentFrame--;
134				this._isDone = (this._currentFrame <= 1);
135			}
136		}
137		
138		/**
139		 * Release reference to creating object.
140		 * Use this to remove a FrameDelay object that is still running when the creating object will be removed.
141		 */
142		override public function destruct():void 
143		{
144			FramePulse.removeEnterFrameListener(handleEnterFrame);
145			
146			this._callback = null;
147			this._params = null;
148			
149			super.destruct();
150		}
151	}
152}