PageRenderTime 0ms CodeModel.GetById 82ms app.highlight 3ms RepoModel.GetById 56ms app.codeStats 0ms

/DiegoRay/actionscripts/org/casaframework/time/FrameDelay.as

https://github.com/joemaffia/flash-junk
ActionScript | 120 lines | 43 code | 12 blank | 65 comment | 4 complexity | 7fdec40afa4a38c1fd847c64182ac2e9 MD5 | raw file
  1/*
  2	CASA Framework for ActionScript 2.0
  3	Copyright (C) 2007  CASA Framework
  4	http://casaframework.org
  5	
  6	This library is free software; you can redistribute it and/or
  7	modify it under the terms of the GNU Lesser General Public
  8	License as published by the Free Software Foundation; either
  9	version 2.1 of the License, or (at your option) any later version.
 10	
 11	This library is distributed in the hope that it will be useful,
 12	but WITHOUT ANY WARRANTY; without even the implied warranty of
 13	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14	Lesser General Public License for more details.
 15	
 16	You should have received a copy of the GNU Lesser General Public
 17	License along with this library; if not, write to the Free Software
 18	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 19*/
 20
 21import org.casaframework.core.CoreObject;
 22import org.casaframework.control.RunnableInterface;
 23import org.casaframework.time.EnterFrame;
 24
 25/**
 26	Creates a callback after one or more frames have been fired. Helps prevent race conditions by allowing recently created MovieClips, Classed, etc... a frame to initialize before proceeding. Should only need a single frame because <code>onEnterFrame</code> should only occur when all frame jobs are finished.
 27
 28	@author Aaron Clinger
 29	@version 12/14/06
 30	@since Flash Player 7
 31	@example
 32		<code>
 33			class Example {
 34				public function Example() {
 35					// A lot of inits, attachMovies, etc...
 36					
 37					var initDelay:FrameDelay = new FrameDelay(this, "initComplete");
 38					initDelay.start();
 39				}
 40				
 41				public function initComplete():Void {
 42					// Safe to execute code
 43				}
 44			}
 45		</code>
 46		
 47		When starting a SWF or after attaching a movie:
 48		<code>
 49			var initDelay:FrameDelay = new FrameDelay(this, "initComplete", 1, "Aaron", 1984);
 50			this.initDelay.start();
 51			
 52			function initComplete(firstName:String, birthYear:Number):Void {
 53				trace(firstName + " was born in " + birthYear);
 54			}
 55		</code>
 56*/
 57
 58class org.casaframework.time.FrameDelay extends CoreObject implements RunnableInterface {
 59	private var $scope:Object;
 60	private var $methodName:String;
 61	private var $frames:Number;
 62	private var $fires:Number;
 63	private var $arguments:Array;
 64	private var $enterFrameInstance:EnterFrame;
 65	
 66	/**
 67		Defines {@link FrameDelay} object.
 68		
 69		@param scope: An object that contains the method specified by "methodName".
 70		@param methodName: A method that exists in the scope of the object specified by "scope".
 71		@param frames: <strong>[optional]</strong> The number of frames to wait before calling "methodName". <code>frames</code> defaults to <code>1</code>.
 72		@param param(s): <strong>[optional]</strong> Parameters passed to the function specified by "methodName". Multiple parameters are allowed and should be separated by commas: param1,param2, ...,paramN
 73	*/
 74	public function FrameDelay(scope:Object, methodName:String, frames:Number, param:Object) {
 75		super();
 76		
 77		this.$setClassDescription('org.casaframework.time.FrameDelay');
 78		
 79		this.$scope      = scope;
 80		this.$methodName = methodName;
 81		this.$frames     = (frames == undefined || frames == null) ? 1 : frames;
 82		this.$arguments  = arguments.slice(3);
 83	}
 84	
 85	/**
 86		Starts or restarts the frame delay.
 87	*/
 88	public function start():Void {
 89		this.$fires = 0;
 90		this.$enterFrameInstance = EnterFrame.getInstance();
 91		this.$enterFrameInstance.addEventObserver(this, EnterFrame.EVENT_ENTER_FRAME, '$onFrameFire');
 92	}
 93	
 94	/**
 95		Stops the frame delay from completing.
 96	*/
 97	public function stop():Void {
 98		this.$enterFrameInstance.removeEventObserver(this, EnterFrame.EVENT_ENTER_FRAME, '$onFrameFire');
 99		delete this.$enterFrameInstance;
100		delete this.$fires;
101	}
102	
103	public function destroy():Void {
104		this.stop();
105		
106		delete this.$scope;
107		delete this.$methodName;
108		delete this.$frames;
109		delete this.$arguments;
110		
111		super.destroy();
112	}
113	
114	private function $onFrameFire():Void {
115		if (++this.$fires >= this.$frames) {
116			this.stop();
117			this.$scope[this.$methodName].apply(this.$scope, this.$arguments);
118		}
119	}
120}