PageRenderTime 34ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/temple/utils/types/MovieClipUtils.as

http://github.com/MediaMonks/Temple
ActionScript | 231 lines | 124 code | 21 blank | 86 comment | 32 complexity | cb505497db9e33292d8113bf46ebd236 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. package temple.utils.types
  39. {
  40. import temple.debug.errors.TempleArgumentError;
  41. import temple.debug.errors.throwError;
  42. import temple.debug.getClassName;
  43. import temple.utils.FrameDelay;
  44. import temple.utils.TimeOut;
  45. import flash.display.DisplayObject;
  46. import flash.display.DisplayObjectContainer;
  47. import flash.display.MovieClip;
  48. import flash.events.Event;
  49. import flash.utils.Dictionary;
  50. /**
  51. * This class contains some functions for MovieClips.
  52. *
  53. * @author Arjan van Wijk (arjan at mediamonks dot com)
  54. */
  55. public final class MovieClipUtils
  56. {
  57. /**
  58. * Stores all playing movieclip as [movieclip] = speed
  59. */
  60. private static var _CLIP_DICTIONARY:Dictionary;
  61. /**
  62. * Delays a MovieClip from playing for an amount of frames/miliseconds
  63. *
  64. * @param clip MovieClip
  65. * @param delay Delay in frames (or miliseconds when useFrames is set to false)
  66. * @param useFrames Use frames or miliseconds
  67. */
  68. public static function wait(movieclip:MovieClip, delay:uint, frameBased:Boolean = true):void
  69. {
  70. movieclip.stop();
  71. if (frameBased)
  72. {
  73. new FrameDelay(movieclip.play, delay);
  74. }
  75. else
  76. {
  77. new TimeOut(movieclip.play, delay);
  78. }
  79. }
  80. /**
  81. * Play a MovieClip at a given speed (forwards or backwards)
  82. *
  83. * @param clip MovieClip
  84. * @param speed int speed indication (negative for backwards playing)
  85. */
  86. public static function play(movieclip:MovieClip, speed:int = 1):void
  87. {
  88. MovieClipUtils.stop(movieclip);
  89. if (speed != 0)
  90. {
  91. if (MovieClipUtils._CLIP_DICTIONARY == null) MovieClipUtils._CLIP_DICTIONARY = new Dictionary(true);
  92. movieclip.addEventListener(Event.ENTER_FRAME, handleEnterFrame, false, 0, true);
  93. MovieClipUtils._CLIP_DICTIONARY[movieclip] = speed;
  94. }
  95. }
  96. /**
  97. * Plays a movieclip backwards
  98. */
  99. public static function playBackwards(movieclip:MovieClip):void
  100. {
  101. MovieClipUtils.play(movieclip, -1);
  102. }
  103. private static function handleEnterFrame(event:Event):void
  104. {
  105. var movieclip:MovieClip = MovieClip(event.target);
  106. var speed:int = MovieClipUtils._CLIP_DICTIONARY[movieclip];
  107. movieclip.gotoAndStop(movieclip.currentFrame + speed);
  108. if (movieclip.currentFrame == 1 || movieclip.currentFrame == movieclip.totalFrames)
  109. {
  110. MovieClipUtils.stop(movieclip);
  111. }
  112. }
  113. /**
  114. * Stops a MovieClip and removes the play-enterFrame
  115. *
  116. * @param movieclip the MovieClip to stop
  117. * @param callStop if set to true (default) the 'stop()' method will also be called on the MovieClip.
  118. */
  119. public static function stop(movieclip:MovieClip, callStop:Boolean = true):void
  120. {
  121. if(callStop) movieclip.stop();
  122. if (MovieClipUtils._CLIP_DICTIONARY != null && MovieClipUtils._CLIP_DICTIONARY[movieclip] != null) movieclip.removeEventListener(Event.ENTER_FRAME, handleEnterFrame);
  123. }
  124. /**
  125. * Recursively stop() all nested MovieClips through all clip's DisplayObjectContianers)
  126. *
  127. * Note: Does not affect argument
  128. */
  129. public static function deepStop(clip:DisplayObjectContainer):void
  130. {
  131. if(clip == null) throwError(new TempleArgumentError(MovieClipUtils, 'null clip'));
  132. var num:int = clip.numChildren;
  133. for(var i:int=0;i<num;i++)
  134. {
  135. var disp:DisplayObject = clip.getChildAt(i);
  136. if(disp is DisplayObjectContainer)
  137. {
  138. MovieClipUtils.deepStop(DisplayObjectContainer(disp));
  139. if(disp is MovieClip)
  140. {
  141. MovieClip(disp).stop();
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * Recursively play() all nested MovieClips through all clip's DisplayObjectContianers)
  148. *
  149. * Note: Does not affect argument
  150. */
  151. public static function deepPlay(clip:DisplayObjectContainer):void
  152. {
  153. if(clip == null) throwError(new TempleArgumentError(MovieClipUtils, 'null clip'));
  154. var num:int = clip.numChildren;
  155. for(var i:int=0;i<num;i++)
  156. {
  157. var disp:DisplayObject = clip.getChildAt(i);
  158. if(disp is DisplayObjectContainer)
  159. {
  160. if(disp is MovieClip)
  161. {
  162. MovieClip(disp).play();
  163. }
  164. MovieClipUtils.deepPlay(DisplayObjectContainer(disp));
  165. }
  166. }
  167. }
  168. /**
  169. * Recursively nextFrames all nested MovieClips through all children DisplayObjectContianers), with option for looping
  170. *
  171. * Usefull in a enterFrame if a gotoAndStop on parent timeline stops nested anims
  172. *
  173. * Last-resort util: may screw up synced nested anims
  174. *
  175. * Note: Does not affect argument
  176. */
  177. public static function deepNextFrame(clip:DisplayObjectContainer, loop:Boolean=false):void
  178. {
  179. if(clip == null) throwError(new TempleArgumentError(MovieClipUtils, 'Clip cannot be null'));
  180. var num:int = clip.numChildren;
  181. for(var i:int=0;i<num;i++)
  182. {
  183. var disp:DisplayObject = clip.getChildAt(i);
  184. if(disp is DisplayObjectContainer)
  185. {
  186. if(disp is MovieClip)
  187. {
  188. var mc:MovieClip = MovieClip(disp);
  189. if(mc.currentFrame == mc.totalFrames)
  190. {
  191. if(loop)
  192. {
  193. mc.gotoAndStop(1);
  194. }
  195. }
  196. else
  197. {
  198. mc.nextFrame();
  199. }
  200. }
  201. MovieClipUtils.deepNextFrame(DisplayObjectContainer(disp), loop);
  202. }
  203. }
  204. }
  205. public static function toString():String
  206. {
  207. return getClassName(MovieClipUtils);
  208. }
  209. }
  210. }