CCAnimation.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * <p>
  24. * cc.AnimationFrame
  25. * A frame of the animation. It contains information like:
  26. * - sprite frame name
  27. * - # of delay units.
  28. * - offset
  29. * </p>
  30. * @class
  31. * @extends cc.Class
  32. */
  33. cc.AnimationFrame = cc.Class.extend(/** @lends cc.AnimationFrame# */{
  34. _spriteFrame:null,
  35. _delayPerUnit:0,
  36. _userInfo:null,
  37. ctor:function () {
  38. this._delayPerUnit = 0;
  39. },
  40. clone: function(){
  41. var frame = new cc.AnimationFrame();
  42. frame.initWithSpriteFrame(this._spriteFrame.clone(), this._delayPerUnit, this._userInfo);
  43. return frame;
  44. },
  45. copyWithZone:function (pZone) {
  46. return cc.clone(this);
  47. },
  48. copy:function (pZone) {
  49. var newFrame = new cc.AnimationFrame();
  50. newFrame.initWithSpriteFrame(this._spriteFrame.clone(), this._delayPerUnit, this._userInfo);
  51. return newFrame;
  52. },
  53. /**
  54. * initializes the animation frame with a spriteframe, number of delay units and a notification user info
  55. * @param {cc.SpriteFrame} spriteFrame
  56. * @param {Number} delayUnits
  57. * @param {object} userInfo
  58. */
  59. initWithSpriteFrame:function (spriteFrame, delayUnits, userInfo) {
  60. this._spriteFrame = spriteFrame;
  61. this._delayPerUnit = delayUnits;
  62. this._userInfo = userInfo;
  63. return true;
  64. },
  65. /**
  66. * cc.SpriteFrameName to be used
  67. * @return {cc.SpriteFrame}
  68. */
  69. getSpriteFrame:function () {
  70. return this._spriteFrame;
  71. },
  72. /**
  73. * cc.SpriteFrameName to be used
  74. * @param {cc.SpriteFrame} spriteFrame
  75. */
  76. setSpriteFrame:function (spriteFrame) {
  77. this._spriteFrame = spriteFrame;
  78. },
  79. /**
  80. * how many units of time the frame takes getter
  81. * @return {Number}
  82. */
  83. getDelayUnits:function () {
  84. return this._delayPerUnit;
  85. },
  86. /**
  87. * how many units of time the frame takes setter
  88. * @param delayUnits
  89. */
  90. setDelayUnits:function (delayUnits) {
  91. this._delayPerUnit = delayUnits;
  92. },
  93. /**
  94. * <p>A cc.AnimationFrameDisplayedNotification notification will be broadcasted when the frame is displayed with this dictionary as UserInfo.<br/>
  95. * If UserInfo is nil, then no notification will be broadcasted. </p>
  96. * @return {object}
  97. */
  98. getUserInfo:function () {
  99. return this._userInfo;
  100. },
  101. /**
  102. * @param {object} userInfo
  103. */
  104. setUserInfo:function (userInfo) {
  105. this._userInfo = userInfo;
  106. }
  107. });
  108. /**
  109. * <p>
  110. * A cc.Animation object is used to perform animations on the cc.Sprite objects.<br/>
  111. * <br/>
  112. * The cc.Animation object contains cc.SpriteFrame objects, and a possible delay between the frames. <br/>
  113. * You can animate a cc.Animation object by using the cc.Animate action. Example: <br/>
  114. * </p>
  115. * @class
  116. * @extends cc.Class
  117. *
  118. * @example
  119. * //create an animation object
  120. * var animation = cc.Animation.create();
  121. *
  122. * //add a sprite frame to this animation
  123. * animation.addFrameWithFile("grossini_dance_01.png");
  124. *
  125. * //create an animate with this animation
  126. * var action = cc.Animate.create(animation);
  127. *
  128. * //run animate
  129. * this._grossini.runAction(action);
  130. */
  131. cc.Animation = cc.Class.extend(/** @lends cc.Animation# */{
  132. _frames:null,
  133. _loops:0,
  134. _restoreOriginalFrame:false,
  135. _duration:0,
  136. _delayPerUnit:0,
  137. _totalDelayUnits:0,
  138. /**
  139. * Constructor
  140. */
  141. ctor:function () {
  142. this._frames = [];
  143. },
  144. // attributes
  145. /**
  146. * return array of CCAnimationFrames
  147. * @return {Array}
  148. */
  149. getFrames:function () {
  150. return this._frames;
  151. },
  152. /**
  153. * array of CCAnimationFrames setter
  154. * @param {Array} frames
  155. */
  156. setFrames:function (frames) {
  157. this._frames = frames;
  158. },
  159. /**
  160. * adds a frame to a cc.Animation The frame will be added with one "delay unit".
  161. * @param {cc.SpriteFrame} frame
  162. */
  163. addSpriteFrame:function (frame) {
  164. var animFrame = new cc.AnimationFrame();
  165. animFrame.initWithSpriteFrame(frame, 1, null);
  166. this._frames.push(animFrame);
  167. // update duration
  168. this._totalDelayUnits++;
  169. },
  170. /**
  171. * Adds a frame with an image filename. Internally it will create a cc.SpriteFrame and it will add it. The frame will be added with one "delay unit".
  172. * @param {String} fileName
  173. */
  174. addSpriteFrameWithFile:function (fileName) {
  175. var texture = cc.TextureCache.getInstance().addImage(fileName);
  176. var rect = cc.RectZero();
  177. rect._size = texture.getContentSize();
  178. var frame = cc.SpriteFrame.createWithTexture(texture, rect);
  179. this.addSpriteFrame(frame);
  180. },
  181. /**
  182. * Adds a frame with a texture and a rect. Internally it will create a cc.SpriteFrame and it will add it. The frame will be added with one "delay unit".
  183. * @param {cc.Texture2D} texture
  184. * @param {cc.Rect} rect
  185. */
  186. addSpriteFrameWithTexture:function (texture, rect) {
  187. var pFrame = cc.SpriteFrame.createWithTexture(texture, rect);
  188. this.addSpriteFrame(pFrame);
  189. },
  190. /**
  191. * Initializes a cc.Animation with cc.AnimationFrame
  192. * @param {Array} arrayOfAnimationFrames
  193. * @param {Number} delayPerUnit
  194. * @param {Number} loops
  195. */
  196. initWithAnimationFrames:function (arrayOfAnimationFrames, delayPerUnit, loops) {
  197. cc.ArrayVerifyType(arrayOfAnimationFrames, cc.AnimationFrame);
  198. this._delayPerUnit = delayPerUnit;
  199. this._loops = loops;
  200. this._totalDelayUnits = 0;
  201. var locFrames = this._frames;
  202. locFrames.length = 0;
  203. for (var i = 0; i < arrayOfAnimationFrames.length; i++) {
  204. var animFrame = arrayOfAnimationFrames[i];
  205. locFrames.push(animFrame);
  206. this._totalDelayUnits += animFrame.getDelayUnits();
  207. }
  208. return true;
  209. },
  210. clone: function(){
  211. var animation = new cc.Animation();
  212. animation.initWithAnimationFrames(this._copyFrames(), this._delayPerUnit, this._loops);
  213. animation.setRestoreOriginalFrame(this._restoreOriginalFrame);
  214. return animation;
  215. },
  216. /**
  217. * @param {cc.Animation} pZone
  218. */
  219. copyWithZone:function (pZone) {
  220. var pCopy = new cc.Animation();
  221. pCopy.initWithAnimationFrames(this._copyFrames(), this._delayPerUnit, this._loops);
  222. pCopy.setRestoreOriginalFrame(this._restoreOriginalFrame);
  223. return pCopy;
  224. },
  225. _copyFrames:function(){
  226. var copyFrames = [];
  227. for(var i = 0; i< this._frames.length;i++)
  228. copyFrames.push(this._frames[i].clone());
  229. return copyFrames;
  230. },
  231. copy:function (pZone) {
  232. return this.copyWithZone(null);
  233. },
  234. /**
  235. * return how many times the animation is going to loop. 0 means animation is not animated. 1, animation is executed one time, ...
  236. * @return {Number}
  237. */
  238. getLoops:function () {
  239. return this._loops;
  240. },
  241. /**
  242. * set how many times the animation is going to loop. 0 means animation is not animated. 1, animation is executed one time, ...
  243. * @param {Number} value
  244. */
  245. setLoops:function (value) {
  246. this._loops = value;
  247. },
  248. /**
  249. * whether or not it shall restore the original frame when the animation finishes
  250. * @param {Boolean} restOrigFrame
  251. */
  252. setRestoreOriginalFrame:function (restOrigFrame) {
  253. this._restoreOriginalFrame = restOrigFrame;
  254. },
  255. /**
  256. * return whether or not it shall restore the original frame when the animation finishes
  257. * @return {Boolean}
  258. */
  259. getRestoreOriginalFrame:function () {
  260. return this._restoreOriginalFrame;
  261. },
  262. /**
  263. * return duration in seconds of the whole animation. It is the result of totalDelayUnits * delayPerUnit
  264. * @return {Number}
  265. */
  266. getDuration:function () {
  267. return this._totalDelayUnits * this._delayPerUnit;
  268. },
  269. /**
  270. * return Delay in seconds of the "delay unit"
  271. * @return {Number}
  272. */
  273. getDelayPerUnit:function () {
  274. return this._delayPerUnit;
  275. },
  276. /**
  277. * set Delay in seconds of the "delay unit"
  278. * @param {Number} delayPerUnit
  279. */
  280. setDelayPerUnit:function (delayPerUnit) {
  281. this._delayPerUnit = delayPerUnit;
  282. },
  283. /**
  284. * return total Delay units of the cc.Animation.
  285. * @return {Number}
  286. */
  287. getTotalDelayUnits:function () {
  288. return this._totalDelayUnits;
  289. },
  290. /**
  291. * Initializes a cc.Animation with frames and a delay between frames
  292. * @param {Array} frames
  293. * @param {Number} delay
  294. */
  295. initWithSpriteFrames:function (frames, delay) {
  296. cc.ArrayVerifyType(frames, cc.SpriteFrame);
  297. this._loops = 1;
  298. delay = delay || 0;
  299. this._delayPerUnit = delay;
  300. this._totalDelayUnits = 0;
  301. var locFrames = this._frames;
  302. locFrames.length = 0;
  303. if (frames) {
  304. for (var i = 0; i < frames.length; i++) {
  305. var frame = frames[i];
  306. var animFrame = new cc.AnimationFrame();
  307. animFrame.initWithSpriteFrame(frame, 1, null);
  308. locFrames.push(animFrame);
  309. }
  310. this._totalDelayUnits += frames.length;
  311. }
  312. return true;
  313. },
  314. /**
  315. * Currently JavaScript Bindings (JSB), in some cases, needs to use retain and release. This is a bug in JSB,
  316. * and the ugly workaround is to use retain/release. So, these 2 methods were added to be compatible with JSB.
  317. * This is a hack, and should be removed once JSB fixes the retain/release bug
  318. */
  319. retain:function () {
  320. },
  321. release:function () {
  322. }
  323. });
  324. /**
  325. * Creates an animation.
  326. * @param {Array} frames
  327. * @param {Number} delay
  328. * @param {Number} loops
  329. * @return {cc.Animation}
  330. * @example
  331. * //Creates an animation
  332. * var animation1 = cc.Animation.create();
  333. *
  334. * //Create an animation with sprite frames
  335. * var animFrames = [];
  336. * var frame = cache.getSpriteFrame("grossini_dance_01.png");
  337. * animFrames.push(frame);
  338. * var animation2 = cc.Animation.create(animFrames);
  339. *
  340. * //Create an animation with sprite frames and delay
  341. * var animation3 = cc.Animation.create(animFrames, 0.2);
  342. */
  343. cc.Animation.create = function (frames, delay, loops) {
  344. var len = arguments.length;
  345. var animation = new cc.Animation();
  346. if (len == 0) {
  347. animation.initWithSpriteFrames(null, 0);
  348. } else if (len == 2) {
  349. /** with frames and a delay between frames */
  350. delay = delay || 0;
  351. animation.initWithSpriteFrames(frames, delay);
  352. } else if (len == 3) {
  353. animation.initWithAnimationFrames(frames, delay, loops);
  354. }
  355. return animation;
  356. };
  357. /**
  358. * Creates an animation with an array of cc.AnimationFrame, the delay per units in seconds and and how many times it should be executed.
  359. * @param {Array} arrayOfAnimationFrameNames
  360. * @param {Number} delayPerUnit
  361. * @param {Number} loops
  362. * @return {cc.Animation}
  363. */
  364. cc.Animation.createWithAnimationFrames = function (arrayOfAnimationFrameNames, delayPerUnit, loops) {
  365. var animation = new cc.Animation();
  366. animation.initWithAnimationFrames(arrayOfAnimationFrameNames, delayPerUnit, loops);
  367. return animation;
  368. };