CCAnimationCache.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies 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.animationCache is a singleton object 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. * <br/>
  27. * example<br/>
  28. * cc.animationCache.addAnimation(animation,"animation1");<br/>
  29. * </p>
  30. * @class
  31. * @name cc.animationCache
  32. */
  33. cc.animationCache = /** @lends cc.animationCache# */{
  34. _animations: {},
  35. /**
  36. * Adds a cc.Animation with a name.
  37. * @param {cc.Animation} animation
  38. * @param {String} name
  39. */
  40. addAnimation:function (animation, name) {
  41. this._animations[name] = animation;
  42. },
  43. /**
  44. * Deletes a cc.Animation from the cache.
  45. * @param {String} name
  46. */
  47. removeAnimation:function (name) {
  48. if (!name) {
  49. return;
  50. }
  51. if (this._animations[name]) {
  52. delete this._animations[name];
  53. }
  54. },
  55. /**
  56. * <p>
  57. * Returns a cc.Animation that was previously added.<br/>
  58. * If the name is not found it will return nil.<br/>
  59. * You should retain the returned copy if you are going to use it.</br>
  60. * </p>
  61. * @param {String} name
  62. * @return {cc.Animation}
  63. */
  64. getAnimation:function (name) {
  65. if (this._animations[name])
  66. return this._animations[name];
  67. return null;
  68. },
  69. _addAnimationsWithDictionary:function (dictionary,plist) {
  70. var animations = dictionary["animations"];
  71. if (!animations) {
  72. cc.log(cc._LogInfos.animationCache__addAnimationsWithDictionary);
  73. return;
  74. }
  75. var version = 1;
  76. var properties = dictionary["properties"];
  77. if (properties) {
  78. version = (properties["format"] != null) ? parseInt(properties["format"]) : version;
  79. var spritesheets = properties["spritesheets"];
  80. var spriteFrameCache = cc.spriteFrameCache;
  81. var path = cc.path;
  82. for (var i = 0; i < spritesheets.length; i++) {
  83. spriteFrameCache.addSpriteFrames(path.changeBasename(plist, spritesheets[i]));
  84. }
  85. }
  86. switch (version) {
  87. case 1:
  88. this._parseVersion1(animations);
  89. break;
  90. case 2:
  91. this._parseVersion2(animations);
  92. break;
  93. default :
  94. cc.log(cc._LogInfos.animationCache__addAnimationsWithDictionary_2);
  95. break;
  96. }
  97. },
  98. /**
  99. * <p>
  100. * Adds an animations from a plist file.<br/>
  101. * Make sure that the frames were previously loaded in the cc.SpriteFrameCache.
  102. * </p>
  103. * @param {String} plist
  104. */
  105. addAnimations:function (plist) {
  106. cc.assert(plist, cc._LogInfos.animationCache_addAnimations_2);
  107. var dict = cc.loader.getRes(plist);
  108. if(!dict){
  109. cc.log(cc._LogInfos.animationCache_addAnimations);
  110. return;
  111. }
  112. this._addAnimationsWithDictionary(dict,plist);
  113. },
  114. _parseVersion1:function (animations) {
  115. var frameCache = cc.spriteFrameCache;
  116. for (var key in animations) {
  117. var animationDict = animations[key];
  118. var frameNames = animationDict["frames"];
  119. var delay = parseFloat(animationDict["delay"]) || 0;
  120. var animation = null;
  121. if (!frameNames) {
  122. cc.log(cc._LogInfos.animationCache__parseVersion1, key);
  123. continue;
  124. }
  125. var frames = [];
  126. for (var i = 0; i < frameNames.length; i++) {
  127. var spriteFrame = frameCache.getSpriteFrame(frameNames[i]);
  128. if (!spriteFrame) {
  129. cc.log(cc._LogInfos.animationCache__parseVersion1_2, key, frameNames[i]);
  130. continue;
  131. }
  132. var animFrame = new cc.AnimationFrame();
  133. animFrame.initWithSpriteFrame(spriteFrame, 1, null);
  134. frames.push(animFrame);
  135. }
  136. if (frames.length === 0) {
  137. cc.log(cc._LogInfos.animationCache__parseVersion1_3, key);
  138. continue;
  139. } else if (frames.length != frameNames.length) {
  140. cc.log(cc._LogInfos.animationCache__parseVersion1_4, key);
  141. }
  142. animation = cc.Animation.create(frames, delay, 1);
  143. cc.animationCache.addAnimation(animation, key);
  144. }
  145. },
  146. _parseVersion2:function (animations) {
  147. var frameCache = cc.spriteFrameCache;
  148. for (var key in animations) {
  149. var animationDict = animations[key];
  150. var isLoop = animationDict["loop"];
  151. var loopsTemp = parseInt(animationDict["loops"]);
  152. var loops = isLoop ? cc.REPEAT_FOREVER : ((isNaN(loopsTemp)) ? 1 : loopsTemp);
  153. var restoreOriginalFrame = (animationDict["restoreOriginalFrame"] && animationDict["restoreOriginalFrame"] == true) ? true : false;
  154. var frameArray = animationDict["frames"];
  155. if (!frameArray) {
  156. cc.log(cc._LogInfos.animationCache__parseVersion2, key);
  157. continue;
  158. }
  159. //Array of AnimationFrames
  160. var arr = [];
  161. for (var i = 0; i < frameArray.length; i++) {
  162. var entry = frameArray[i];
  163. var spriteFrameName = entry["spriteframe"];
  164. var spriteFrame = frameCache.getSpriteFrame(spriteFrameName);
  165. if (!spriteFrame) {
  166. cc.log(cc._LogInfos.animationCache__parseVersion2_2, key, spriteFrameName);
  167. continue;
  168. }
  169. var delayUnits = parseFloat(entry["delayUnits"]) || 0;
  170. var userInfo = entry["notification"];
  171. var animFrame = new cc.AnimationFrame();
  172. animFrame.initWithSpriteFrame(spriteFrame, delayUnits, userInfo);
  173. arr.push(animFrame);
  174. }
  175. var delayPerUnit = parseFloat(animationDict["delayPerUnit"]) || 0;
  176. var animation = new cc.Animation();
  177. animation.initWithAnimationFrames(arr, delayPerUnit, loops);
  178. animation.setRestoreOriginalFrame(restoreOriginalFrame);
  179. cc.animationCache.addAnimation(animation, key);
  180. }
  181. },
  182. _clear: function () {
  183. this._animations = {};
  184. }
  185. };