PageRenderTime 62ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/DiegoRay/actionscripts/org/casaframework/time/FrameDelay.as

https://github.com/joemaffia/flash-junk
ActionScript | 120 lines | 43 code | 12 blank | 65 comment | 4 complexity | 7fdec40afa4a38c1fd847c64182ac2e9 MD5 | raw file
  1. /*
  2. CASA Framework for ActionScript 2.0
  3. Copyright (C) 2007 CASA Framework
  4. http://casaframework.org
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. import org.casaframework.core.CoreObject;
  18. import org.casaframework.control.RunnableInterface;
  19. import org.casaframework.time.EnterFrame;
  20. /**
  21. Creates a callback after one or more frames have been fired. Helps prevent race conditions by allowing recently created MovieClips, Classed, etc... a frame to initialize before proceeding. Should only need a single frame because <code>onEnterFrame</code> should only occur when all frame jobs are finished.
  22. @author Aaron Clinger
  23. @version 12/14/06
  24. @since Flash Player 7
  25. @example
  26. <code>
  27. class Example {
  28. public function Example() {
  29. // A lot of inits, attachMovies, etc...
  30. var initDelay:FrameDelay = new FrameDelay(this, "initComplete");
  31. initDelay.start();
  32. }
  33. public function initComplete():Void {
  34. // Safe to execute code
  35. }
  36. }
  37. </code>
  38. When starting a SWF or after attaching a movie:
  39. <code>
  40. var initDelay:FrameDelay = new FrameDelay(this, "initComplete", 1, "Aaron", 1984);
  41. this.initDelay.start();
  42. function initComplete(firstName:String, birthYear:Number):Void {
  43. trace(firstName + " was born in " + birthYear);
  44. }
  45. </code>
  46. */
  47. class org.casaframework.time.FrameDelay extends CoreObject implements RunnableInterface {
  48. private var $scope:Object;
  49. private var $methodName:String;
  50. private var $frames:Number;
  51. private var $fires:Number;
  52. private var $arguments:Array;
  53. private var $enterFrameInstance:EnterFrame;
  54. /**
  55. Defines {@link FrameDelay} object.
  56. @param scope: An object that contains the method specified by "methodName".
  57. @param methodName: A method that exists in the scope of the object specified by "scope".
  58. @param frames: <strong>[optional]</strong> The number of frames to wait before calling "methodName". <code>frames</code> defaults to <code>1</code>.
  59. @param param(s): <strong>[optional]</strong> Parameters passed to the function specified by "methodName". Multiple parameters are allowed and should be separated by commas: param1,param2, ...,paramN
  60. */
  61. public function FrameDelay(scope:Object, methodName:String, frames:Number, param:Object) {
  62. super();
  63. this.$setClassDescription('org.casaframework.time.FrameDelay');
  64. this.$scope = scope;
  65. this.$methodName = methodName;
  66. this.$frames = (frames == undefined || frames == null) ? 1 : frames;
  67. this.$arguments = arguments.slice(3);
  68. }
  69. /**
  70. Starts or restarts the frame delay.
  71. */
  72. public function start():Void {
  73. this.$fires = 0;
  74. this.$enterFrameInstance = EnterFrame.getInstance();
  75. this.$enterFrameInstance.addEventObserver(this, EnterFrame.EVENT_ENTER_FRAME, '$onFrameFire');
  76. }
  77. /**
  78. Stops the frame delay from completing.
  79. */
  80. public function stop():Void {
  81. this.$enterFrameInstance.removeEventObserver(this, EnterFrame.EVENT_ENTER_FRAME, '$onFrameFire');
  82. delete this.$enterFrameInstance;
  83. delete this.$fires;
  84. }
  85. public function destroy():Void {
  86. this.stop();
  87. delete this.$scope;
  88. delete this.$methodName;
  89. delete this.$frames;
  90. delete this.$arguments;
  91. super.destroy();
  92. }
  93. private function $onFrameFire():Void {
  94. if (++this.$fires >= this.$frames) {
  95. this.stop();
  96. this.$scope[this.$methodName].apply(this.$scope, this.$arguments);
  97. }
  98. }
  99. }