CCAnimationCache.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. * Singleton that manages the Animations.<br/>
  25. * It saves in a cache the animations. You should use this class if you want to save your animations in a cache.<br/>
  26. * </p>
  27. * @class
  28. * @extends cc.Class
  29. *
  30. * @example
  31. * cc.AnimationCache.getInstance().addAnimation(animation,"animation1");
  32. */
  33. cc.AnimationCache = cc.Class.extend(/** @lends cc.AnimationCache# */{
  34. /**
  35. * Adds a cc.Animation with a name.
  36. * @param {cc.Animation} animation
  37. * @param {String} name
  38. */
  39. addAnimation:function (animation, name) {
  40. this._animations[name] = animation;
  41. },
  42. /**
  43. * Deletes a cc.Animation from the cache.
  44. * @param {String} name
  45. */
  46. removeAnimation:function (name) {
  47. if (!name) {
  48. return;
  49. }
  50. if (this._animations[name]) {
  51. delete this._animations[name];
  52. }
  53. },
  54. /**
  55. * <p>
  56. * Returns a cc.Animation that was previously added.<br/>
  57. * If the name is not found it will return nil.<br/>
  58. * You should retain the returned copy if you are going to use it.</br>
  59. * </p>
  60. * @param {String} name
  61. * @return {cc.Animation}
  62. */
  63. getAnimation:function (name) {
  64. if (this._animations[name])
  65. return this._animations[name];
  66. return null;
  67. },
  68. /**
  69. * <p>
  70. * Adds an animation from an NSDictionary<br/>
  71. * Make sure that the frames were previously loaded in the cc.SpriteFrameCache.
  72. * </p>
  73. * @param {object} dictionary
  74. * @param {String} plist
  75. */
  76. _addAnimationsWithDictionary:function (dictionary,plist) {
  77. var animations = dictionary["animations"];
  78. if (!animations) {
  79. cc.log("cocos2d: cc.AnimationCache: No animations were found in provided dictionary.");
  80. return;
  81. }
  82. var version = 1;
  83. var properties = dictionary["properties"];
  84. if (properties) {
  85. version = (properties["format"] != null) ? parseInt(properties["format"]) : version;
  86. var spritesheets = properties["spritesheets"];
  87. var spriteFrameCache = cc.SpriteFrameCache.getInstance();
  88. var fileUtils = cc.FileUtils.getInstance(), path;
  89. for (var i = 0; i < spritesheets.length; i++) {
  90. path = fileUtils.fullPathFromRelativeFile(spritesheets[i], plist);
  91. spriteFrameCache.addSpriteFrames(path);
  92. }
  93. }
  94. switch (version) {
  95. case 1:
  96. this._parseVersion1(animations);
  97. break;
  98. case 2:
  99. this._parseVersion2(animations);
  100. break;
  101. default :
  102. cc.log("cc.AnimationCache. Invalid animation format");
  103. break;
  104. }
  105. },
  106. /**
  107. * <p>
  108. * Adds an animation from a plist file.<br/>
  109. * Make sure that the frames were previously loaded in the cc.SpriteFrameCache.
  110. * </p>
  111. * @param {String} plist
  112. */
  113. addAnimations:function (plist) {
  114. if(!plist)
  115. throw "cc.AnimationCache.addAnimations(): Invalid texture file name";
  116. var fileUtils = cc.FileUtils.getInstance();
  117. var path = fileUtils.fullPathForFilename(plist);
  118. var dict = fileUtils.dictionaryWithContentsOfFileThreadSafe(path);
  119. if(!dict){
  120. cc.log("cc.AnimationCache.addAnimations(): File could not be found");
  121. return;
  122. }
  123. this._addAnimationsWithDictionary(dict,plist);
  124. },
  125. _parseVersion1:function (animations) {
  126. var frameCache = cc.SpriteFrameCache.getInstance();
  127. for (var key in animations) {
  128. var animationDict = animations[key];
  129. var frameNames = animationDict["frames"];
  130. var delay = parseFloat(animationDict["delay"]) || 0;
  131. var animation = null;
  132. if (!frameNames) {
  133. cc.log("cocos2d: cc.AnimationCache: Animation '" + key + "' found in dictionary without any frames - cannot add to animation cache.");
  134. continue;
  135. }
  136. var frames = [];
  137. for (var i = 0; i < frameNames.length; i++) {
  138. var spriteFrame = frameCache.getSpriteFrame(frameNames[i]);
  139. if (!spriteFrame) {
  140. cc.log("cocos2d: cc.AnimationCache: Animation '" + key + "' refers to frame '" + frameNames[i]
  141. + "' which is not currently in the cc.SpriteFrameCache. This frame will not be added to the animation.");
  142. continue;
  143. }
  144. var animFrame = new cc.AnimationFrame();
  145. animFrame.initWithSpriteFrame(spriteFrame, 1, null);
  146. frames.push(animFrame);
  147. }
  148. if (frames.length === 0) {
  149. cc.log("cocos2d: cc.AnimationCache: None of the frames for animation '" + key
  150. + "' were found in the cc.SpriteFrameCache. Animation is not being added to the Animation Cache.");
  151. continue;
  152. } else if (frames.length != frameNames.length) {
  153. cc.log("cocos2d: cc.AnimationCache: An animation in your dictionary refers to a frame which is not in the cc.SpriteFrameCache." +
  154. " Some or all of the frames for the animation '" + key + "' may be missing.");
  155. }
  156. animation = cc.Animation.createWithAnimationFrames(frames, delay, 1);
  157. cc.AnimationCache.getInstance().addAnimation(animation, key);
  158. }
  159. },
  160. _parseVersion2:function (animations) {
  161. var frameCache = cc.SpriteFrameCache.getInstance();
  162. for (var key in animations) {
  163. var animationDict = animations[key];
  164. var isLoop = animationDict["loop"];
  165. var loopsTemp = parseInt(animationDict["loops"]);
  166. var loops = isLoop ? cc.REPEAT_FOREVER : ((isNaN(loopsTemp)) ? 1 : loopsTemp);
  167. var restoreOriginalFrame = (animationDict["restoreOriginalFrame"] && animationDict["restoreOriginalFrame"] == true) ? true : false;
  168. var frameArray = animationDict["frames"];
  169. if (!frameArray) {
  170. cc.log("cocos2d: CCAnimationCache: Animation '" + key + "' found in dictionary without any frames - cannot add to animation cache.");
  171. continue;
  172. }
  173. //Array of AnimationFrames
  174. var arr = [];
  175. for (var i = 0; i < frameArray.length; i++) {
  176. var entry = frameArray[i];
  177. var spriteFrameName = entry["spriteframe"];
  178. var spriteFrame = frameCache.getSpriteFrame(spriteFrameName);
  179. if (!spriteFrame) {
  180. cc.log("cocos2d: cc.AnimationCache: Animation '" + key + "' refers to frame '" + spriteFrameName
  181. + "' which is not currently in the cc.SpriteFrameCache. This frame will not be added to the animation.");
  182. continue;
  183. }
  184. var delayUnits = parseFloat(entry["delayUnits"]) || 0;
  185. var userInfo = entry["notification"];
  186. var animFrame = new cc.AnimationFrame();
  187. animFrame.initWithSpriteFrame(spriteFrame, delayUnits, userInfo);
  188. arr.push(animFrame);
  189. }
  190. var delayPerUnit = parseFloat(animationDict["delayPerUnit"]) || 0;
  191. var animation = new cc.Animation();
  192. animation.initWithAnimationFrames(arr, delayPerUnit, loops);
  193. animation.setRestoreOriginalFrame(restoreOriginalFrame);
  194. cc.AnimationCache.getInstance().addAnimation(animation, key);
  195. }
  196. },
  197. /**
  198. * initialize cc.AnimationCache
  199. * @return {Boolean}
  200. */
  201. init:function () {
  202. this._animations = {};
  203. return true;
  204. },
  205. _animations:null
  206. });
  207. /**
  208. * Purges the cache. It releases all the cc.Animation objects and the shared instance.
  209. */
  210. cc.AnimationCache.purgeSharedAnimationCache = function () {
  211. if (cc.s_sharedAnimationCache) {
  212. cc.s_sharedAnimationCache._animations = null;
  213. cc.s_sharedAnimationCache = null;
  214. }
  215. };
  216. /**
  217. * Retruns ths shared instance of the Animation cache
  218. * @return {cc.AnimationCache}
  219. */
  220. cc.AnimationCache.getInstance = function () {
  221. if (cc.s_sharedAnimationCache === null) {
  222. cc.s_sharedAnimationCache = new cc.AnimationCache();
  223. cc.s_sharedAnimationCache.init();
  224. }
  225. return cc.s_sharedAnimationCache;
  226. };
  227. cc.s_sharedAnimationCache = null;