PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 36ms app.codeStats 0ms

/flash/3.4/src/org/casalib/time/FrameDelay.as

https://github.com/galahad9/pubnub-api
ActionScript | 132 lines | 41 code | 14 blank | 77 comment | 1 complexity | 728098af24e4a029ebb6e756b4b810ac MD5 | raw file
  1/*
  2	CASA Lib for ActionScript 3.0
  3	Copyright (c) 2011, Aaron Clinger & Contributors of CASA Lib
  4	All rights reserved.
  5	
  6	Redistribution and use in source and binary forms, with or without
  7	modification, are permitted provided that the following conditions are met:
  8	
  9	- Redistributions of source code must retain the above copyright notice,
 10	  this list of conditions and the following disclaimer.
 11	
 12	- Redistributions in binary form must reproduce the above copyright notice,
 13	  this list of conditions and the following disclaimer in the documentation
 14	  and/or other materials provided with the distribution.
 15	
 16	- Neither the name of the CASA Lib nor the names of its contributors
 17	  may be used to endorse or promote products derived from this software
 18	  without specific prior written permission.
 19	
 20	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 24	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30	POSSIBILITY OF SUCH DAMAGE.
 31*/
 32package org.casalib.time {
 33	import flash.events.Event;
 34	import org.casalib.process.Process;
 35	import org.casalib.time.EnterFrame;
 36	
 37	/**
 38		Creates a callback after one or more frames. The class helps prevent race conditions by allowing recently created MovieClips, Classes, etc. a frame to initialize before proceeding.
 39		
 40		@author Aaron Clinger
 41		@version 02/11/10
 42		@example
 43			<code>
 44				package {
 45					import org.casalib.display.CasaMovieClip;
 46					import org.casalib.time.FrameDelay;
 47					
 48					
 49					public class MyExample extends CasaMovieClip {
 50						protected var _frameDelay:FrameDelay;
 51						
 52						
 53						public function MyExample() {
 54							super();
 55							
 56							this._frameDelay = new FrameDelay(this._onInitComplete);
 57							this._frameDelay.start();
 58						}
 59						
 60						protected function _onInitComplete():void{
 61							this._frameDelay.destroy();
 62							this._frameDelay = null;
 63							
 64							trace("Ready!");
 65						}
 66					}
 67				}
 68			</code>
 69	*/
 70	public class FrameDelay extends Process {
 71		protected var _arguments:Array;
 72		protected var _callBack:Function;
 73		protected var _count:uint;
 74		protected var _frames:uint;
 75		protected var _enterFrameInstance:EnterFrame;
 76		
 77		
 78		/**
 79			Runs a function at a specified periodic interval.
 80			
 81			@param closure: The function to execute.
 82			@param frames: The amount of frames to delay.
 83			@param arguments: Arguments to be passed to the function when executed.
 84		*/
 85		public function FrameDelay(closure:Function, frames:uint = 1, ...arguments) {
 86			super();
 87			
 88			this._callBack           = closure;
 89			this._frames             = frames;
 90			this._arguments          = arguments;
 91			this._enterFrameInstance = EnterFrame.getInstance();
 92		}
 93		
 94		/**
 95			Starts or restarts the delay.
 96		*/
 97		override public function start():void {
 98			super.start();
 99			
100			this._count = 0;
101			this._enterFrameInstance.addEventListener(Event.ENTER_FRAME, this._onEnterFrame, false, 0, true);
102		}
103		
104		/**
105			Stops the delay from completing.
106		*/
107		override public function stop():void {
108			super.stop();
109			
110			this._enterFrameInstance.removeEventListener(Event.ENTER_FRAME, this._onEnterFrame);
111		}
112		
113		override public function destroy():void {
114			this._enterFrameInstance.removeEventListener(Event.ENTER_FRAME, this._onEnterFrame);
115			
116			this._arguments = null;
117			this._callBack  = null;
118			
119			super.destroy();
120		}
121		
122		protected function _onEnterFrame(e:Event):void {
123			if (++this._count >= this._frames) {
124				this._enterFrameInstance.removeEventListener(Event.ENTER_FRAME, this._onEnterFrame);
125				
126				this._callBack.apply(null, this._arguments);
127				
128				this._complete();
129			}
130		}
131	}
132}