PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/as/gs/util/FrameDelay.as

https://github.com/zeratel/guttershark
ActionScript | 91 lines | 44 code | 9 blank | 38 comment | 5 complexity | d2e48459e749d4db582e9dcecf4fd1a2 MD5 | raw file
  1. package gs.util
  2. {
  3. import flash.events.Event;
  4. /**
  5. * The FrameDelay class allows for a callback to be called after
  6. * a certain amount of frames have passed.
  7. *
  8. * @example Using a FrameDelay:
  9. * <listing>
  10. * var fd:FrameDelay=new FrameDelay(after10, 10);
  11. * function after10():void
  12. * {
  13. * trace("10 frames have passed");
  14. * }
  15. * </listing>
  16. */
  17. final public class FrameDelay
  18. {
  19. /**
  20. * is done flag.
  21. */
  22. private var isDone:Boolean;
  23. /**
  24. * The current frame.
  25. */
  26. private var currentFrame:int;
  27. /**
  28. * The callback.
  29. */
  30. private var callback:Function;
  31. /**
  32. * Parameters to send to the callback.
  33. */
  34. private var params:Array;
  35. /**
  36. * Constructor for FrameDelay instances.
  37. *
  38. * @param callback The callback to call.
  39. * @param frameCount The number of frames to wait.
  40. * @param params An array of parameters to send to your callback.
  41. */
  42. public function FrameDelay(callback:Function,frameCount:int=0,params:Array=null)
  43. {
  44. currentFrame=frameCount;
  45. this.callback=callback;
  46. this.params=params;
  47. isDone=(isNaN(frameCount) || (frameCount <= 1));
  48. FramePulse.AddEnterFrameListener(handleEnterFrame);
  49. }
  50. /**
  51. * Handle an enter frame event.
  52. */
  53. private function handleEnterFrame(e:Event):void
  54. {
  55. if(isDone)
  56. {
  57. if(params==null)callback();
  58. else callback.apply(null,params);
  59. dispose();
  60. }
  61. else
  62. {
  63. currentFrame--;
  64. isDone=(currentFrame<=1);
  65. }
  66. }
  67. /**
  68. * Dispose of this FrameDelay instance.
  69. */
  70. public function dispose():void
  71. {
  72. if(isDone)
  73. {
  74. FramePulse.RemoveEnterFrameListener(handleEnterFrame);
  75. params=null;
  76. callback=null;
  77. currentFrame=0;
  78. isDone=false;
  79. }
  80. }
  81. }
  82. }