PageRenderTime 45ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/asaplibrary/util/FrameDelay.as

http://asaplibrary.googlecode.com/
ActionScript | 90 lines | 34 code | 4 blank | 52 comment | 7 complexity | 459aa9c2d34cbf1cb8386d4dfe1ad9a9 MD5 | raw file
  1. /*
  2. Copyright 2007-2011 by the authors of asaplibrary, http://asaplibrary.org
  3. Copyright 2005-2007 by the authors of asapframework, http://asapframework.org
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package org.asaplibrary.util {
  15. import flash.events.Event;
  16. /**
  17. 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.
  18. @usage
  19. To execute function 'init' after 1 frame, use:
  20. <code>
  21. new FrameDelay(init);
  22. </code>
  23. To execute function 'init' after 10 frames, use:
  24. <code>
  25. new FrameDelay(init, 10);
  26. </code>
  27. To call function 'setProps' with parameters, executed after 1 frame:
  28. <code>
  29. new FrameDelay(setProps, 1, [shape, 'alpha', 0]);
  30. </code>
  31. <code>
  32. private function setProps (inShape:Shape, inProperty:String, inValue:Number) : void {
  33. inShape[inProperty] == inValue);
  34. }
  35. </code>
  36. */
  37. public class FrameDelay {
  38. private var mIsDone : Boolean = false;
  39. private var mCurrentFrame : int;
  40. private var mCallback : Function;
  41. private var mParams : Array;
  42. /**
  43. Creates a new FrameDelay. Starts the delay immediately.
  44. @param inCallback: the callback function to be called when done waiting
  45. @param inFrameCount: the number of frames to wait; when left out, or set to 1 or 0, one frame is waited
  46. @param inParams: list of parameters to pass to the callback function
  47. */
  48. public function FrameDelay(inCallback : Function, inFrameCount : int = 0, inParams : Array = null) {
  49. mCurrentFrame = inFrameCount;
  50. mCallback = inCallback;
  51. mParams = inParams;
  52. mIsDone = (isNaN(inFrameCount) || (inFrameCount <= 1));
  53. FramePulse.addEnterFrameListener(handleEnterFrame);
  54. }
  55. /**
  56. Release reference to creating object.
  57. Use this to remove a FrameDelay object that is still running when the creating object will be removed.
  58. */
  59. public function die() : void {
  60. if (!mIsDone) {
  61. FramePulse.removeEnterFrameListener(handleEnterFrame);
  62. }
  63. }
  64. /**
  65. Handle the onEnterFrame event.
  66. Checks if still waiting - when true: calls callback function.
  67. @param e: not used
  68. */
  69. private function handleEnterFrame(e : Event) : void {
  70. if (mIsDone) {
  71. FramePulse.removeEnterFrameListener(handleEnterFrame);
  72. if (mParams == null) {
  73. mCallback();
  74. } else {
  75. mCallback.apply(null, mParams);
  76. }
  77. } else {
  78. mCurrentFrame--;
  79. mIsDone = (mCurrentFrame <= 1);
  80. }
  81. }
  82. }
  83. }