API Documentation for: 2.3.1
Show:

File:CharacterController.js

  1. /**
  2. * @module cloudkid
  3. */
  4. (function(){
  5.  
  6. "use strict";
  7.  
  8. // Imports
  9. var Animator = cloudkid.Animator;
  10. /**
  11. * Character Controller class is designed to play animated
  12. * sequences on the timeline. This is a flexible way to
  13. * animate characters on a timeline
  14. *
  15. * @class CharacterController
  16. */
  17. var CharacterController = function()
  18. {
  19. this.initialize();
  20. };
  21. var p = CharacterController.prototype;
  22. /**
  23. * The current stack of animations to play
  24. *
  25. * @property {Array} _animationStack
  26. * @private
  27. */
  28. p._animationStack = null;
  29. /**
  30. * The currently playing animation
  31. *
  32. * @property {CharacterClip} _currentAnimation
  33. * @private
  34. */
  35. p._currentAnimation = null;
  36. /**
  37. * Current number of loops for the current animation
  38. *
  39. * @property {int} _loops
  40. * @private
  41. */
  42. p._loops = 0;
  43. /**
  44. * If the current animation choreographies can't be interrupted
  45. *
  46. * @property {bool} _interruptable
  47. * @private
  48. */
  49. p._interruptable = true;
  50. /**
  51. * If frame dropping is allowed for this animation set
  52. *
  53. * @property {bool} _allowFrameDropping
  54. * @private
  55. */
  56. p._allowFrameDropping = false;
  57. /**
  58. * The current character
  59. *
  60. * @property {createjs.MovieClip} _character
  61. * @private
  62. */
  63. p._character = null;
  64. /**
  65. * Callback function for playing animation
  66. *
  67. * @property {function} _callback
  68. * @private
  69. */
  70. p._callback = null;
  71. /**
  72. * If this instance has been destroyed
  73. *
  74. * @property {bool} _destroyed
  75. * @private
  76. */
  77. p._destroyed = false;
  78. /**
  79. * Initiliazes this Character controller
  80. *
  81. * @function initialize
  82. */
  83. p.initialize = function()
  84. {
  85. this._animationStack = [];
  86. };
  87. /**
  88. * Set the current character, setting to null clears character
  89. *
  90. * @function setCharacter
  91. * @param {createjs.MovieClip} character MovieClip
  92. */
  93. p.setCharacter = function(character)
  94. {
  95. this.clear();
  96. this._character = character;
  97. if (this._character)
  98. {
  99. Debug.assert(this._character instanceof createjs.MovieClip, "character must subclass MovieClip");
  100. this._character.stop();
  101. }
  102. };
  103. /**
  104. * If we want to play a static frame
  105. *
  106. * @function gotoFrameAndStop
  107. * @param {String} event The frame label to stop on
  108. */
  109. p.gotoFrameAndStop = function(event)
  110. {
  111. Debug.assert(this._character, "gotoFrameAndStop() requires a character!");
  112. Animator.stop(this._character);
  113. this._animationStack.length = 0;
  114. this._character.gotoAndStop(event);
  115. };
  116. /**
  117. * Will play a sequence of animations
  118. *
  119. * @function playClips
  120. * @param {Array} clips an array of CharacterClip objects
  121. * @param {function} callback Callback for when the animations are either done, or
  122. * have been interrupted. Will pass true is interrupted,
  123. * false if they completed
  124. * @param {bool} interruptable If calling this can interrupt the current animation(s)
  125. * @param {bool} cancelPreviousCallback Cancel the callback the last time this was called
  126. * @param {bool} allowFrameDropping If frame dropping is allowed for this frame, if the Animator is doing frame drop checks
  127. */
  128. p.playClips = function(clips, callback, interruptable, cancelPreviousCallback, allowFrameDropping)
  129. {
  130. callback = callback || null;
  131. interruptable = interruptable || true;
  132. cancelPreviousCallback = cancelPreviousCallback || true;
  133. allowFrameDropping = allowFrameDropping || true;
  134. Debug.assert(this._character, "playClips requires a character!");
  135. if (!this._interruptable) return;
  136. Animator.stop(this._character);
  137. this._interruptable = interruptable;
  138. if (this._callback && !cancelPreviousCallback)
  139. {
  140. this._callback(true);
  141. }
  142. this._callback = callback;
  143. this._animationStack.length = 0;
  144. for(var c in clips)
  145. {
  146. this._animationStack.push(clips[c]);
  147. }
  148. this._allowFrameDropping = allowFrameDropping;
  149. this.startNext();
  150. };
  151. /**
  152. * Start the next animation in the sequence
  153. *
  154. * @function startNext
  155. */
  156. p.startNext = function()
  157. {
  158. this._loops = 0;
  159. if (this._animationStack.length > 0)
  160. {
  161. this._currentAnimation = this._animationStack.shift();
  162. Animator.play(
  163. this._character,
  164. this._currentAnimation.event,
  165. this._animationComplete.bind(this),
  166. [this],
  167. this._allowFrameDropping
  168. );
  169. }
  170. else if(this._callback)
  171. {
  172. this._interruptable = true;
  173. var cb = this._callback;
  174. this._callback = null;
  175. cb(false);
  176. }
  177. };
  178. /**
  179. * When the animation has completed playing
  180. *
  181. * @function _animationComplete
  182. * @private
  183. */
  184. p._animationComplete = function()
  185. {
  186. this._loops++;
  187. if(this._currentAnimation.loops === 0 || this._loops < this._currentAnimation.loops)
  188. {
  189. Animator.play(
  190. this._character,
  191. this._currentAnimation.event,
  192. this._animationComplete.bind(this),
  193. null,
  194. this._allowFrameDropping
  195. );
  196. }
  197. else if (this._currentAnimation.loops == this._loops)
  198. {
  199. this.startNext();
  200. }
  201. };
  202. /**
  203. * Clear any animations for the current character
  204. *
  205. * @function clear
  206. */
  207. p.clear = function()
  208. {
  209. if (this._character)
  210. {
  211. Animator.stop(this._character);
  212. }
  213. this._currentAnimation = null;
  214. this._interruptable = true;
  215. this._callback = null;
  216. this._animationStack.length = 0;
  217. this._loops = 0;
  218. };
  219. /**
  220. * Don't use after this
  221. *
  222. * @function destroy
  223. */
  224. p.destroy = function()
  225. {
  226. if(this._destroyed) return;
  227. this._destroyed = true;
  228. this.clear();
  229. this._character = null;
  230. this._animationStack = null;
  231. };
  232. // Assign to the cloudkid namespace
  233. namespace('cloudkid').CharacterController = CharacterController;
  234. }());