PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/brentbushnell/pubnub-api
ActionScript | 132 lines | 41 code | 14 blank | 77 comment | 1 complexity | 728098af24e4a029ebb6e756b4b810ac MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. CASA Lib for ActionScript 3.0
  3. Copyright (c) 2011, 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. package org.casalib.time {
  28. import flash.events.Event;
  29. import org.casalib.process.Process;
  30. import org.casalib.time.EnterFrame;
  31. /**
  32. 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.
  33. @author Aaron Clinger
  34. @version 02/11/10
  35. @example
  36. <code>
  37. package {
  38. import org.casalib.display.CasaMovieClip;
  39. import org.casalib.time.FrameDelay;
  40. public class MyExample extends CasaMovieClip {
  41. protected var _frameDelay:FrameDelay;
  42. public function MyExample() {
  43. super();
  44. this._frameDelay = new FrameDelay(this._onInitComplete);
  45. this._frameDelay.start();
  46. }
  47. protected function _onInitComplete():void{
  48. this._frameDelay.destroy();
  49. this._frameDelay = null;
  50. trace("Ready!");
  51. }
  52. }
  53. }
  54. </code>
  55. */
  56. public class FrameDelay extends Process {
  57. protected var _arguments:Array;
  58. protected var _callBack:Function;
  59. protected var _count:uint;
  60. protected var _frames:uint;
  61. protected var _enterFrameInstance:EnterFrame;
  62. /**
  63. Runs a function at a specified periodic interval.
  64. @param closure: The function to execute.
  65. @param frames: The amount of frames to delay.
  66. @param arguments: Arguments to be passed to the function when executed.
  67. */
  68. public function FrameDelay(closure:Function, frames:uint = 1, ...arguments) {
  69. super();
  70. this._callBack = closure;
  71. this._frames = frames;
  72. this._arguments = arguments;
  73. this._enterFrameInstance = EnterFrame.getInstance();
  74. }
  75. /**
  76. Starts or restarts the delay.
  77. */
  78. override public function start():void {
  79. super.start();
  80. this._count = 0;
  81. this._enterFrameInstance.addEventListener(Event.ENTER_FRAME, this._onEnterFrame, false, 0, true);
  82. }
  83. /**
  84. Stops the delay from completing.
  85. */
  86. override public function stop():void {
  87. super.stop();
  88. this._enterFrameInstance.removeEventListener(Event.ENTER_FRAME, this._onEnterFrame);
  89. }
  90. override public function destroy():void {
  91. this._enterFrameInstance.removeEventListener(Event.ENTER_FRAME, this._onEnterFrame);
  92. this._arguments = null;
  93. this._callBack = null;
  94. super.destroy();
  95. }
  96. protected function _onEnterFrame(e:Event):void {
  97. if (++this._count >= this._frames) {
  98. this._enterFrameInstance.removeEventListener(Event.ENTER_FRAME, this._onEnterFrame);
  99. this._callBack.apply(null, this._arguments);
  100. this._complete();
  101. }
  102. }
  103. }
  104. }