PageRenderTime 47ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/DiegoRay/actionscripts/org/asapframework/util/FrameDelay.as

https://github.com/joemaffia/flash-junk
ActionScript | 125 lines | 36 code | 14 blank | 75 comment | 5 complexity | 3d31d02931f8331da72d9b8c278499a7 MD5 | raw file
  1. /*
  2. Copyright 2005-2006 by the authors of asapframework, http://asapframework.org
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // ASAP classes
  14. import org.asapframework.events.EventDelegate;
  15. import org.asapframework.util.framepulse.FramePulse;
  16. /**
  17. Class that provides one or more frames delay.
  18. Use this when initializing a swf or a bunch of movieclips, to enable the player to do its thing.
  19. Usually a single frame delay will do the job, since the next enterFrame will come when all other jobs are finished.
  20. This class will catch that next onEnterFrame and fire the function in the object passed as parameters.
  21. A parameter is available if the delay has to be more than one frame.
  22. @usage
  23. <code>
  24. class myClass {
  25. private var mFrameDelay:FrameDelay;
  26. function init () : Void {
  27. ... do a bunch of inits
  28. // wait one enterFrame
  29. mFrameDelay = new FrameDelay(this, initDone);
  30. }
  31. private function initDone () : Void {
  32. ...
  33. }
  34. private function goAway () {
  35. mFrameDelay.die();
  36. }
  37. </code>
  38. When starting a swf:
  39. <code>
  40. var lc:LocalController = new MyLocalController(this);
  41. // wait one enterFrame before notifying the MovieManager that this movie is done initializing
  42. var fd:FrameDelay = new FrameDelay(lc, lc.notifyMovieInitialized);
  43. </code>
  44. */
  45. class org.asapframework.util.FrameDelay {
  46. private var mIsDone:Boolean = false;
  47. private var mCurrentFrame:Number;
  48. private var mSender:Object;
  49. private var mCallback:Function;
  50. private var mParams:Array;
  51. private var mFramePulseListener:Function;
  52. /**
  53. Constructor; starts the waiting immediately.
  54. @param inSender:Object, the class that contains the function to be called when done waiting
  55. @param inCallback:Function, the callback function to be called when done waiting
  56. @param inParams:Array, list of paramters to pass to callback function
  57. @param inFrameCount:Number, the number of frames to wait; when left out, or set to 1 or 0, one frame is waited
  58. */
  59. public function FrameDelay (inSender:Object, inCallback:Function, inFrameCount:Number, inParams:Array) {
  60. // create handler for enterFrame events
  61. mFramePulseListener = EventDelegate.create(this, onEnterFrame);
  62. wait(inSender, inCallback, inFrameCount, inParams);
  63. }
  64. /**
  65. * Release reference to creating object
  66. * Use this to remove a FrameDelay object that is still running when the creating object will be removed
  67. */
  68. public function die () : Void {
  69. if (!mIsDone) {
  70. FramePulse.removeEnterFrameListener(mFramePulseListener);
  71. }
  72. }
  73. // PRIVATE METHODS
  74. /**
  75. Stores input parameters (see {@link #FrameDelay constructor} parameters), start waiting.
  76. */
  77. private function wait (inSender:Object, inCallback:Function, inFrameCount:Number, inParams:Array) : Void {
  78. mCurrentFrame = inFrameCount;
  79. mSender = inSender;
  80. mCallback = inCallback;
  81. mParams = inParams;
  82. mIsDone = ((inFrameCount == undefined) || (inFrameCount <= 1));
  83. // listen to framepulse events
  84. FramePulse.addEnterFrameListener(mFramePulseListener);
  85. }
  86. /**
  87. Handle the onEnterFrame event.
  88. Checks if still waiting - when true: calls callback function.
  89. */
  90. private function onEnterFrame () : Void {
  91. if (mIsDone) {
  92. FramePulse.removeEnterFrameListener(mFramePulseListener);
  93. mCallback.apply(mSender,mParams);
  94. } else {
  95. mCurrentFrame--;
  96. mIsDone = (mCurrentFrame <= 1);
  97. }
  98. }
  99. }