PageRenderTime 45ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/casalib/time/FrameDelay.hx

http://github.com/andyli/casahx
Haxe | 133 lines | 41 code | 15 blank | 77 comment | 1 complexity | e5c5dfaa438908dcffd45a57ad8d9892 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. 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.core.UInt;
  30. import org.casalib.process.Process;
  31. import org.casalib.time.EnterFrame;
  32. /**
  33. 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.
  34. @author Aaron Clinger
  35. @version 02/11/10
  36. @example
  37. <code>
  38. package {
  39. import org.casalib.display.CasaMovieClip;
  40. import org.casalib.time.FrameDelay;
  41. public class MyExample extends CasaMovieClip {
  42. protected var _frameDelay:FrameDelay;
  43. public function MyExample() {
  44. super();
  45. this._frameDelay = new FrameDelay(this._onInitComplete);
  46. this._frameDelay.start();
  47. }
  48. protected function _onInitComplete():void{
  49. this._frameDelay.destroy();
  50. this._frameDelay = null;
  51. trace("Ready!");
  52. }
  53. }
  54. }
  55. </code>
  56. */
  57. class FrameDelay extends Process {
  58. var _arguments:Array<Dynamic>;
  59. var _callBack:Dynamic;
  60. var _count:UInt;
  61. var _frames:UInt;
  62. var _enterFrameInstance:EnterFrame;
  63. /**
  64. Runs a function at a specified periodic interval.
  65. @param closure: The function to execute.
  66. @param frames: The amount of frames to delay.
  67. @param arguments: Arguments to be passed to the function when executed.
  68. */
  69. public function new(closure:Dynamic, ?frames:UInt = 1, arguments:Array<Dynamic>) {
  70. super();
  71. this._callBack = closure;
  72. this._frames = frames;
  73. this._arguments = arguments;
  74. this._enterFrameInstance = EnterFrame.getInstance();
  75. }
  76. /**
  77. Starts or restarts the delay.
  78. */
  79. override public function start():Void {
  80. super.start();
  81. this._count = 0;
  82. this._enterFrameInstance.addEventListener(Event.ENTER_FRAME, this._onEnterFrame, false, 0, true);
  83. }
  84. /**
  85. Stops the delay from completing.
  86. */
  87. override public function stop():Void {
  88. super.stop();
  89. this._enterFrameInstance.removeEventListener(Event.ENTER_FRAME, this._onEnterFrame);
  90. }
  91. override public function destroy():Void {
  92. this._enterFrameInstance.removeEventListener(Event.ENTER_FRAME, this._onEnterFrame);
  93. this._arguments = null;
  94. this._callBack = null;
  95. super.destroy();
  96. }
  97. function _onEnterFrame(e:Event):Void {
  98. if (++_count >= untyped _frames) {
  99. this._enterFrameInstance.removeEventListener(Event.ENTER_FRAME, this._onEnterFrame);
  100. Reflect.callMethod(this,_callBack,this._arguments);
  101. this._complete();
  102. }
  103. }
  104. }