PageRenderTime 49ms CodeModel.GetById 25ms 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. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. - Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. - Neither the name of the CASA Lib nor the names of its contributors
  13. may be used to endorse or promote products derived from this software
  14. without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. import org.casalib.core.CoreObject;
  28. import org.casalib.control.RunnableInterface;
  29. import org.casalib.time.EnterFrame;
  30. /**
  31. 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.
  32. @author Aaron Clinger
  33. @version 03/21/08
  34. @since Flash Player 7
  35. @example
  36. <code>
  37. class Example {
  38. public function Example() {
  39. // A lot of inits, attachMovies, etc...
  40. var initDelay:FrameDelay = new FrameDelay(this, "initComplete");
  41. initDelay.start();
  42. }
  43. public function initComplete():Void {
  44. // Safe to execute code
  45. }
  46. }
  47. </code>
  48. When starting a SWF or after attaching a movie:
  49. <code>
  50. var initDelay:FrameDelay = new FrameDelay(this, "initComplete", 1, "Aaron", 1984);
  51. this.initDelay.start();
  52. function initComplete(firstName:String, birthYear:Number):Void {
  53. trace(firstName + " was born in " + birthYear);
  54. }
  55. </code>
  56. */
  57. class org.casalib.time.FrameDelay extends CoreObject implements RunnableInterface {
  58. private var $scope:Object;
  59. private var $methodName:String;
  60. private var $frames:Number;
  61. private var $fires:Number;
  62. private var $arguments:Array;
  63. private var $enterFrameInstance:EnterFrame;
  64. /**
  65. Defines {@link FrameDelay} object.
  66. @param scope: An object that contains the method specified by "methodName".
  67. @param methodName: A method that exists in the scope of the object specified by "scope".
  68. @param frames: <strong>[optional]</strong> The number of frames to wait before calling "methodName". <code>frames</code> defaults to <code>1</code>.
  69. @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
  70. */
  71. public function FrameDelay(scope:Object, methodName:String, frames:Number, param:Object) {
  72. super();
  73. this.$setClassDescription('org.casalib.time.FrameDelay');
  74. this.$scope = scope;
  75. this.$methodName = methodName;
  76. this.$frames = (frames == undefined || frames == null) ? 1 : frames;
  77. this.$arguments = arguments.slice(3);
  78. }
  79. /**
  80. Starts or restarts the frame delay.
  81. */
  82. public function start():Void {
  83. this.$fires = 0;
  84. this.$enterFrameInstance = EnterFrame.getInstance();
  85. this.$enterFrameInstance.addEventObserver(this, EnterFrame.EVENT_ENTER_FRAME, '$onFrameFire');
  86. }
  87. /**
  88. Stops the frame delay from completing.
  89. */
  90. public function stop():Void {
  91. this.$enterFrameInstance.removeEventObserver(this, EnterFrame.EVENT_ENTER_FRAME, '$onFrameFire');
  92. delete this.$enterFrameInstance;
  93. delete this.$fires;
  94. }
  95. public function destroy():Void {
  96. this.stop();
  97. delete this.$scope;
  98. delete this.$methodName;
  99. delete this.$frames;
  100. delete this.$arguments;
  101. super.destroy();
  102. }
  103. private function $onFrameFire():Void {
  104. if (++this.$fires >= this.$frames) {
  105. this.stop();
  106. this.$scope[this.$methodName].apply(this.$scope, this.$arguments);
  107. }
  108. }
  109. }