PageRenderTime 1ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/PLANTILLA CLÁSICA 3/EDICION/scripts/casalib/org/casalib/time/FrameDelay.as

http://flash-web.googlecode.com/
ActionScript | 132 lines | 43 code | 12 blank | 77 comment | 4 complexity | c25e9579e1ee46c3cd39c664a4e6764d MD5 | raw file
  1/*
  2	CASA Lib for ActionScript 2.0
  3	Copyright (c) 2008, 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*/
 32
 33import org.casalib.core.CoreObject;
 34import org.casalib.control.RunnableInterface;
 35import org.casalib.time.EnterFrame;
 36
 37/**
 38	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.
 39	
 40	@author Aaron Clinger
 41	@version 03/21/08
 42	@since Flash Player 7
 43	@example
 44		<code>
 45			class Example {
 46				public function Example() {
 47					// A lot of inits, attachMovies, etc...
 48					
 49					var initDelay:FrameDelay = new FrameDelay(this, "initComplete");
 50					initDelay.start();
 51				}
 52				
 53				public function initComplete():Void {
 54					// Safe to execute code
 55				}
 56			}
 57		</code>
 58		
 59		When starting a SWF or after attaching a movie:
 60		<code>
 61			var initDelay:FrameDelay = new FrameDelay(this, "initComplete", 1, "Aaron", 1984);
 62			this.initDelay.start();
 63			
 64			function initComplete(firstName:String, birthYear:Number):Void {
 65				trace(firstName + " was born in " + birthYear);
 66			}
 67		</code>
 68*/
 69
 70class org.casalib.time.FrameDelay extends CoreObject implements RunnableInterface {
 71	private var $scope:Object;
 72	private var $methodName:String;
 73	private var $frames:Number;
 74	private var $fires:Number;
 75	private var $arguments:Array;
 76	private var $enterFrameInstance:EnterFrame;
 77	
 78	/**
 79		Defines {@link FrameDelay} object.
 80		
 81		@param scope: An object that contains the method specified by "methodName".
 82		@param methodName: A method that exists in the scope of the object specified by "scope".
 83		@param frames: <strong>[optional]</strong> The number of frames to wait before calling "methodName". <code>frames</code> defaults to <code>1</code>.
 84		@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
 85	*/
 86	public function FrameDelay(scope:Object, methodName:String, frames:Number, param:Object) {
 87		super();
 88		
 89		this.$setClassDescription('org.casalib.time.FrameDelay');
 90		
 91		this.$scope      = scope;
 92		this.$methodName = methodName;
 93		this.$frames     = (frames == undefined || frames == null) ? 1 : frames;
 94		this.$arguments  = arguments.slice(3);
 95	}
 96	
 97	/**
 98		Starts or restarts the frame delay.
 99	*/
100	public function start():Void {
101		this.$fires = 0;
102		this.$enterFrameInstance = EnterFrame.getInstance();
103		this.$enterFrameInstance.addEventObserver(this, EnterFrame.EVENT_ENTER_FRAME, '$onFrameFire');
104	}
105	
106	/**
107		Stops the frame delay from completing.
108	*/
109	public function stop():Void {
110		this.$enterFrameInstance.removeEventObserver(this, EnterFrame.EVENT_ENTER_FRAME, '$onFrameFire');
111		delete this.$enterFrameInstance;
112		delete this.$fires;
113	}
114	
115	public function destroy():Void {
116		this.stop();
117		
118		delete this.$scope;
119		delete this.$methodName;
120		delete this.$frames;
121		delete this.$arguments;
122		
123		super.destroy();
124	}
125	
126	private function $onFrameFire():Void {
127		if (++this.$fires >= this.$frames) {
128			this.stop();
129			this.$scope[this.$methodName].apply(this.$scope, this.$arguments);
130		}
131	}
132}