PageRenderTime 64ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/temple/utils/FrameDelay.as

http://github.com/MediaMonks/Temple
ActionScript | 152 lines | 47 code | 11 blank | 94 comment | 6 complexity | 8d693d55fdb83f8b2ee6badc37f0aac6 MD5 | raw file
  1. /*
  2. *
  3. * Temple Library for ActionScript 3.0
  4. * Copyright Š 2010 MediaMonks B.V.
  5. * All rights reserved.
  6. *
  7. * http://code.google.com/p/templelibrary/
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * - Neither the name of the Temple Library nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. *
  24. * Temple Library is free software: you can redistribute it and/or modify
  25. * it under the terms of the GNU Lesser General Public License as published by
  26. * the Free Software Foundation, either version 3 of the License, or
  27. * (at your option) any later version.
  28. *
  29. * Temple Library is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU Lesser General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU Lesser General Public License
  35. * along with Temple Library. If not, see <http://www.gnu.org/licenses/>.
  36. *
  37. */
  38. /*
  39. Copyright 2007 by the authors of asaplibrary, http://asaplibrary.org
  40. Copyright 2005-2007 by the authors of asapframework, http://asapframework.org
  41. Licensed under the Apache License, Version 2.0 (the "License");
  42. you may not use this file except in compliance with the License.
  43. You may obtain a copy of the License at
  44. http://www.apache.org/licenses/LICENSE-2.0
  45. Unless required by applicable law or agreed to in writing, software
  46. distributed under the License is distributed on an "AS IS" BASIS,
  47. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  48. See the License for the specific language governing permissions and
  49. limitations under the License.
  50. */
  51. package temple.utils
  52. {
  53. import temple.core.CoreObject;
  54. import flash.events.Event;
  55. /**
  56. * Delay a function call with one or more frames. Use this when initializing a SWF or a bunch of DisplayObjects, to enable the player to do its thing. Usually a single frame delay will do the job, since the next enterFrame will come when all other jobs are finished.
  57. * To execute function 'init' after 1 frame, use:
  58. *
  59. * @example
  60. * <listing version="3.0">
  61. * new FrameDelay(init);
  62. * </listing>
  63. *
  64. * To execute function 'init' after 10 frames, use:
  65. * <listing version="3.0">
  66. * new FrameDelay(init, 10);
  67. * </listing>
  68. *
  69. * To call function 'setProps' with parameters, executed after 1 frame:
  70. * <listing version="3.0">
  71. * new FrameDelay(setProps, 1, [shape, 'alpha', 0]);
  72. * </listing>
  73. *
  74. * <listing version="3.0">
  75. * private function setProps (shape:Shape, property:String, value:Number):void
  76. * {
  77. * shape[property] = value;
  78. * }
  79. * </listing>
  80. */
  81. public final class FrameDelay extends CoreObject
  82. {
  83. private var _isDone:Boolean = false;
  84. private var _currentFrame:int;
  85. private var _callback:Function;
  86. private var _params:Array;
  87. /**
  88. * Creates a new FrameDelay. Starts the delay immediately.
  89. * @param callback the callback function to be called when done waiting
  90. * @param frameCount the number of frames to wait; when left out, or set to 1 or 0, one frame is waited
  91. * @param params list of parameters to pass to the callback function
  92. */
  93. public function FrameDelay(callback:Function, frameCount:int = 1, params:Array = null)
  94. {
  95. this._currentFrame = frameCount;
  96. this._callback = callback;
  97. this._params = params;
  98. this._isDone = (isNaN(frameCount) || (frameCount <= 1));
  99. FramePulse.addEnterFrameListener(handleEnterFrame);
  100. }
  101. /**
  102. * Handle the Event.ENTER_FRAME event.
  103. * Checks if still waiting - when true: calls callback function.
  104. * @param event not used
  105. */
  106. private function handleEnterFrame(event:Event):void
  107. {
  108. if (this._isDone)
  109. {
  110. FramePulse.removeEnterFrameListener(this.handleEnterFrame);
  111. if (this._params == null)
  112. {
  113. this._callback();
  114. }
  115. else
  116. {
  117. this._callback.apply(null, this._params);
  118. }
  119. }
  120. else
  121. {
  122. this._currentFrame--;
  123. this._isDone = (this._currentFrame <= 1);
  124. }
  125. }
  126. /**
  127. * Release reference to creating object.
  128. * Use this to remove a FrameDelay object that is still running when the creating object will be removed.
  129. */
  130. override public function destruct():void
  131. {
  132. FramePulse.removeEnterFrameListener(handleEnterFrame);
  133. this._callback = null;
  134. this._params = null;
  135. super.destruct();
  136. }
  137. }
  138. }