转自:http://blog.csdn.net/honghaier/article/details/8222401

序列帧动画主要有几个类:

CCSpriteFrame:精灵帧信息,序列帧动画是依靠多个精灵帧信息来显示相应的纹理图像,一个精灵帧信息包包含了所使用的纹理,对应纹理块的位置以及纹理块是否经过旋转和偏移,这些信息可以取得对应纹理中正确的纹理块区域做为精灵帧显示的图像。

CCAnimationFrame:序列帧动画单帧信息,它存储了对应的精灵帧信息。

CCAnimation:序列帧动画信息,它存储了所有的单帧信息,可以对单帧信息进行管理。

CCAnimate:序列帧动画处理类,它是真正完成动画表演的类。

先学习一下CCSpriteFrame:

  1. #ifndef __SPRITE_CCSPRITE_FRAME_H__
  2. #define __SPRITE_CCSPRITE_FRAME_H__
  3. //相关头文件
  4. #include "base_nodes/CCNode.h"
  5. #include "CCProtocols.h"
  6. #include "cocoa/CCObject.h"
  7. #include "cocoa/CCGeometry.h"
  8. //Cocos2d命名空间
  9. NS_CC_BEGIN
  10. //要用到的类
  11. class CCTexture2D;
  12. class CCZone;
  13.  
  14. //精灵帧:精灵序列帧动画中的一帧。
  15. class CC_DLL CCSpriteFrame : public CCObject
  16. {
  17. public:
      // pixel大小
  18. //取得精灵帧的大小(像素单位)
  19.   inline const CCRect& getRectInPixels(void) { return m_obRectInPixels; }
  20.   //设置精灵帧的大小(像素单位)
  21.   void setRectInPixels(const CCRect& rectInPixels);
  22.  
  23.   // 旋转
  24. //取得当前帧所用的纹理块是否旋转了90度
  25.   inline bool isRotated(void) { return m_bRotated; }
  26.   //设置当前帧所用的纹理块旋转90度
  27. inline void setRotated(bool bRotated) { m_bRotated = bRotated; }

  28.   // rect
  29. //取得当前帧所用的纹理块在图集纹理的矩形(点单位)
  30. inline const CCRect& getRect(void) { return m_obRect; }
  31. //设置当前帧所用的纹理块在图集中的矩形(点单位)
  32. void setRect(const CCRect& rect);

  33.   // offset 像素单位
  34. //取得当前帧所用的纹理块在图集中的位置(像素单位)
  35. const CCPoint& getOffsetInPixels(void);
  36. //设置当前帧所用的纹理块在图集中的位置(像素单位)
  37. void setOffsetInPixels(const CCPoint& offsetInPixels);
  1.   // offset 点单位
  2. //取得纹理块在合并时去掉周围空白边后的锚点偏移(点单位).
  3.   const CCPoint& getOffset(void);
  4.   //设置纹理块在合并时去掉周围空白边后的锚点偏移(点单位).
  5. void setOffset(const CCPoint& offsets);
  1.   // originalSize 像素单位
  2. //取得纹理块的原始大小(像素单位)。
  3. inline const CCSize& getOriginalSizeInPixels(void) { return m_obOriginalSizeInPixels; }
  4. //设置纹理块的原始大小(像素单位)。
  5. inline void setOriginalSizeInPixels(const CCSize& sizeInPixels) { m_obOriginalSizeInPixels = sizeInPixels; }

  6.   // originalSize 点单位
  7. //取得纹理块的原始大小(点单位)。
  8. inline const CCSize& getOriginalSize(void) { return m_obOriginalSize; }
  9. //设置纹理块的原始大小(点单位)。
  10. inline void setOriginalSize(const CCSize& sizeInPixels) { m_obOriginalSize = sizeInPixels; }

  11.   // texture
  12. //取得当前帧所使用的图集纹理
  13. CCTexture2D* getTexture(void);
  14. //设置当前帧所使用的图集纹理
  15.    void setTexture(CCTexture2D* pobTexture);
  16.  
  17. public:
  18. //析构
  19.   ~CCSpriteFrame(void);
  20.   //创建一个当前实例的拷贝
  21. virtual CCObject* copyWithZone(CCZone *pZone);
  22. //从一个图集纹理中创建出一帧,内部调用对应createWithTexture函数实现,参一为图集纹理,参二为纹理块矩形。
  23. CC_DEPRECATED_ATTRIBUTE static CCSpriteFrame* frameWithTexture(CCTexture2D* pobTexture, const CCRect& rect);
  24. //从一张图集图片中创建出一帧,内部调用对应create函数实现,参一为图集图片名称,参二为纹理块矩形。
  25. CC_DEPRECATED_ATTRIBUTE static CCSpriteFrame* frameWithTextureFilename(const char* filename, const CCRect& rect);
  26. //从一个图集纹理中创建出一帧,内部调用对应createWithTexture函数实现,参一为图集纹理,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉纹理块空白后所导致的锚点偏移,参五为纹理块的原始大小。
  27. CC_DEPRECATED_ATTRIBUTE static CCSpriteFrame* frameWithTexture(CCTexture2D* pobTexture, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize);
  28. //从一个张图集图片中创建出一帧,内部调用对应create函数实现,参一为图集图片名称,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉纹理块空白后所导致的锚点偏移,参五为纹理块的原始大小。
  29. CC_DEPRECATED_ATTRIBUTE static CCSpriteFrame* frameWithTextureFilename(const char* filename, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize);
  30. //从一张图片文件中以指定的矩形创建一个精灵帧。参一为图集图片名称,参二为对应纹理块的矩形。
  31. static CCSpriteFrame* create(const char* filename, const CCRect& rect);
  32. //从一个图集图片中创建出一帧,参一为图集图片名称,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉纹理块空白后所导致的锚点偏移,参五为纹理块的原始大小。
  33. static CCSpriteFrame* create(const char* filename, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize);
  34. //从一张图集纹理中以指定的矩形创建一个精灵帧。参一为图集纹理,参二为对应纹理块的矩形。
  35. static CCSpriteFrame* createWithTexture(CCTexture2D* pobTexture, const CCRect& rect);
  36.    //从一个图集纹理中创建出一帧,参一为图集纹理,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉纹理块空白后所导致的锚点偏移,参五为纹理块的原始大小。
  37. static CCSpriteFrame* createWithTexture(CCTexture2D* pobTexture, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize);
  38.  
  39. public:
  40. //初始化精灵帧:参一为图集纹理,参二为对应纹理块的矩形。
  41. bool initWithTexture(CCTexture2D* pobTexture, const CCRect& rect);
  42. //初始化精灵帧:参一为图集图片名称,参二为对应纹理块的矩形。
  43. bool initWithTextureFilename(const char* filename, const CCRect& rect);
  44. //初始化精灵帧:参一为图集纹理,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉纹理块空白后所导致的锚点偏移,参五为纹理块的原始大小。
  45. bool initWithTexture(CCTexture2D* pobTexture, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize);
  46. //初始化精灵帧:参一为图集图片名称,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉纹理块空白后所导致的锚点偏移,参五为纹理块的原始大小。
  47. bool initWithTextureFilename(const char* filename, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize);
  48.  
  49. protected:
  50. //去掉纹理块空白后所导致的锚点偏移。
  51.   CCPoint m_obOffset;
  52.   //纹理块的原始大小(未去掉空白的大小)。
  53.   CCSize m_obOriginalSize;
  54.   //纹理块的大小(像素单位)。
  55.   CCRect m_obRectInPixels;
  56.   //矩形块是否旋转。
  57.   bool m_bRotated;
  58.   //纹理块
  59.   CCRect m_obRect;
  60.   //去掉纹理块空白后所导致的锚点偏移(像素单位)。
  61.   CCPoint m_obOffsetInPixels;
  62.   //纹理块的原始大小(像素单位)。
  63.   CCSize m_obOriginalSizeInPixels;
  64.   //图集纹理。
  65.   CCTexture2D *m_pobTexture;
  66.   //对应的图集图片。
  67. std::string m_strTextureFilename;
  68. };
  69.  
  70. NS_CC_END
  71.  
  72. #endif //__SPRITE_CCSPRITE_FRAME_H__

再来看CCSpriteFrame.cpp:

  1. #include "textures/CCTextureCache.h"
  2. #include "CCSpriteFrame.h"
  3. #include "CCDirector.h"
  4.  
  5. //使用Cocos2d命名空间
  6. NS_CC_BEGIN
  7.  
  8. //从一个图集纹理中创建出一帧,内部调用后面的createWithTexture函数实现,参一为图集纹理,参二为纹理块矩形。
  9. CCSpriteFrame* CCSpriteFrame::frameWithTexture(CCTexture2D *pobTexture, const CCRect& rect)
  10. {
  11. return CCSpriteFrame::createWithTexture(pobTexture, rect);
  12. }
  13. //从一个图集纹理中创建出一帧,参一为图集纹理,参二为纹理块矩形。
  14. CCSpriteFrame* CCSpriteFrame::createWithTexture(CCTexture2D *pobTexture, const CCRect& rect)
  15. {
  16. //新帧一个精灵帧,初始化完毕后交由内存管理器进行释放管理。
  17. CCSpriteFrame *pSpriteFrame = new CCSpriteFrame();;
  18. pSpriteFrame->initWithTexture(pobTexture, rect);
  19. pSpriteFrame->autorelease();
  20. //返回新创建的精灵帧。
  21. return pSpriteFrame;
  22. }
  23.  
  24. //从一张图集图片中创建出一帧,内部调用后面的create函数实现,参一为图集图片名称,参二为纹理块矩形。
  25. CCSpriteFrame* CCSpriteFrame::frameWithTextureFilename(const char* filename, const CCRect& rect)
  26. {
  27. return CCSpriteFrame::create(filename, rect);
  28. }
  29. //从一张图集图片中创建出一帧,参一为图集图片名称,参二为纹理块矩形。
  30. CCSpriteFrame* CCSpriteFrame::create(const char* filename, const CCRect& rect)
  31. {
  32.   //新帧一个精灵帧,初始化完毕后交由内存管理器进行释放管理。
  33. CCSpriteFrame *pSpriteFrame = new CCSpriteFrame();;
  34. pSpriteFrame->initWithTextureFilename(filename, rect);
  35. pSpriteFrame->autorelease();
  36. //返回新创建的精灵帧。
  37. return pSpriteFrame;
  38. }
  39.  
  40. //从一个图集纹理中创建出一帧,内部调用后面的createWithTexture函数实现,参一为图集纹理,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉纹理块空白后所导致的锚点偏移,参五为纹理块的原始大小。
  41. CCSpriteFrame* CCSpriteFrame::frameWithTexture(CCTexture2D* pobTexture, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize)
  42. {
  43. return CCSpriteFrame::createWithTexture(pobTexture, rect, rotated, offset, originalSize);
  44. }
  45. //从一个图集纹理中创建出一帧,参一为图集纹理,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉纹理块空白后所导致的锚点偏移,参五为纹理块的原始大小。
  46.  
  47. CCSpriteFrame* CCSpriteFrame::createWithTexture(CCTexture2D* pobTexture, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize)
  48. {
  49.   //新帧一个精灵帧,初始化完毕后交由内存管理器进行释放管理。
  50. CCSpriteFrame *pSpriteFrame = new CCSpriteFrame();;
  51. pSpriteFrame->initWithTexture(pobTexture, rect, rotated, offset, originalSize);
  52. pSpriteFrame->autorelease();
  53. //返回新创建的精灵帧。
  54. return pSpriteFrame;
  55. }
  56.  
  57. //从一个张图集图片中创建出一帧,内部调用后面的create函数实现,参一为图集图片名称,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉纹理块空白后所导致的锚点偏移,参五为纹理块的原始大小。
  58. CCSpriteFrame* CCSpriteFrame::frameWithTextureFilename(const char* filename, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize)
  59. {
  60. return CCSpriteFrame::create(filename, rect, rotated, offset, originalSize);
  61. }
  62. //从一个张图集图片中创建出一帧,参一为图集图片名称,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉纹理块空白后所导致的锚点偏移,参五为纹理块的原始大小。
  63. CCSpriteFrame* CCSpriteFrame::create(const char* filename, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize)
  64. {
  65.   //新帧一个精灵帧,初始化完毕后交由内存管理器进行释放管理。
  66. CCSpriteFrame *pSpriteFrame = new CCSpriteFrame();;
  67. pSpriteFrame->initWithTextureFilename(filename, rect, rotated, offset, originalSize);
  68. pSpriteFrame->autorelease();
  69. //返回新创建的精灵帧。
  70. return pSpriteFrame;
  71. }
  72.  
  73. //初始化精灵帧:参一为图集纹理,参二为对应纹理块的矩形。
  74. bool CCSpriteFrame::initWithTexture(CCTexture2D* pobTexture, const CCRect& rect)
  75. {
  76. //将矩形参数转为当前设备分辨率下的像素单位的大小。
  77.   CCRect rectInPixels = CC_RECT_POINTS_TO_PIXELS(rect);
  78.   //调用参数更详细的初始化函数。
  79. return initWithTexture(pobTexture, rectInPixels, false, CCPointZero, rectInPixels.size);
  80. }
  81. //初始化精灵帧:参一为图集图片名称,参二为对应纹理块的矩形。
  82. bool CCSpriteFrame::initWithTextureFilename(const char* filename, const CCRect& rect)
  83. {
  84. //将矩形参数转为当前设备分辨率下的像素单位的大小。
  85.   CCRect rectInPixels = CC_RECT_POINTS_TO_PIXELS( rect );
  86.   //调用参数更详细的初始化函数。
  87. return initWithTextureFilename(filename, rectInPixels, false, CCPointZero, rectInPixels.size);
  88. }
  89. //初始化精灵帧:参一为图集纹理,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉纹理块空白后所导致的锚点偏移,参五为纹理块的原始大小
  90. bool CCSpriteFrame::initWithTexture(CCTexture2D* pobTexture, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize)
  91. {
  92. //保存图集纹理
  93. m_pobTexture = pobTexture;
  94. //占用纹理资源,对其引用计数器加1
  95. if (pobTexture)
  96. {
  97. pobTexture->retain();
  98. }
  99. //保存像素单位的矩形大小
  100.   m_obRectInPixels = rect;
  101.   //保存点单位的矩形大小
  102.   m_obRect = CC_RECT_PIXELS_TO_POINTS(rect);
  103.   //保存像素单位的去掉纹理块空白后所导致的锚点偏移。
  104.   m_obOffsetInPixels = offset;
  105.   //保存点单位的去掉纹理块空白后所导致的锚点偏移。
  106.   m_obOffset = CC_POINT_PIXELS_TO_POINTS( m_obOffsetInPixels );
  107.   //保存像素单位的纹理块原始大小
  108.   m_obOriginalSizeInPixels = originalSize;
  109.   //保存点单位的纹理块原始大小
  110.   m_obOriginalSize = CC_SIZE_PIXELS_TO_POINTS( m_obOriginalSizeInPixels );
  111.   //保存纹理块是否旋转了90度。
  112. m_bRotated = rotated;
  113.  
  114. return true;
  115. }
  116. //初始化精灵帧:参一为图集图片名称,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉空白后纹理块所导致的偏移,参五为纹理块的原始大小
  117. bool CCSpriteFrame::initWithTextureFilename(const char* filename, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize)
  118. {
  119. //图集纹理置空
  120.   m_pobTexture = NULL;
  121.   //保存图集图片名称。
  122.   m_strTextureFilename = filename;
  123.   //保存像素单位的矩形大小
  124.   m_obRectInPixels = rect;
  125.   //保存转换为点单位的矩形大小
  126.   m_obRect = CC_RECT_PIXELS_TO_POINTS(rect);
  127.   //保存像素单位的去掉纹理块空白后所导致的锚点偏移。
  128.   m_obOffsetInPixels = offset;
  129.   //保存转换为点单位的去掉纹理块空白后所导致的锚点偏移。
  130.   m_obOffset = CC_POINT_PIXELS_TO_POINTS( m_obOffsetInPixels );
  131.   //保存像素单位的纹理块原始大小
  132.   m_obOriginalSizeInPixels = originalSize;
  133.   //保存转换为点单位的纹理块原始大小
  134.   m_obOriginalSize = CC_SIZE_PIXELS_TO_POINTS( m_obOriginalSizeInPixels );
  135.   //保存纹理块是否旋转了90度。
  136. m_bRotated = rotated;
  137.  
  138. return true;
  139. }
  140. //析构
  141. CCSpriteFrame::~CCSpriteFrame(void)
  142. {
  143. CCLOGINFO("cocos2d: deallocing %p", this);
  144. CC_SAFE_RELEASE(m_pobTexture);
  145. }
  146. //创建一个当前实例的拷贝
  147. CCObject* CCSpriteFrame::copyWithZone(CCZone *pZone)
  148. {
  149.   CC_UNUSED_PARAM(pZone);
  150.   //新创一个CCSpriteFrame实例对象。
  151. CCSpriteFrame *pCopy = new CCSpriteFrame();
  152. //以当前实例的各属性值为参数对其进行初始化。
  153.   pCopy->initWithTextureFilename(m_strTextureFilename.c_str(), m_obRectInPixels, m_bRotated, m_obOffsetInPixels, m_obOriginalSizeInPixels);
  154.   //设置同样的纹理图集。
  155.   pCopy->setTexture(m_pobTexture);
  156.   //返回创建的CCSpriteFrame实例对象。
  157. return pCopy;
  158. }
  159. //设置当前帧所用的纹理块在图集中的矩形
  160. void CCSpriteFrame::setRect(const CCRect& rect)
  161. {
  162.   //保存点单位的矩形大小
  163.   m_obRect = rect;
  164.   //保存转换为像素单位的矩形大小
  165. m_obRectInPixels = CC_RECT_POINTS_TO_PIXELS(m_obRect);
  166. }
  167. //设置当前帧所用的纹理块在图集中的矩形(像素单位)
  168. void CCSpriteFrame::setRectInPixels(const CCRect& rectInPixels)
  169. {
  170. //保存像素单位的矩形大小
  171.   m_obRectInPixels = rectInPixels;
  172.   //保存转换为点单位的矩形大小
  173. m_obRect = CC_RECT_PIXELS_TO_POINTS(rectInPixels);
  174. }
  175. //取得纹理块在合并时去掉周围空白边后的锚点偏移(点单位).
  176. const CCPoint& CCSpriteFrame::getOffset(void)
  177. {
  178. return m_obOffset;
  179. }
  180. //设置纹理块在合并时去掉周围空白边后的锚点偏移(点单位).
  181. void CCSpriteFrame::setOffset(const CCPoint& offsets)
  182. {
  183. //保存纹理块在合并时去掉周围空白边后的锚点偏移(点单位).
  184.   m_obOffset = offsets;
  185.   //保存转换为像素单位的纹理块在合并时去掉周围空白边后的锚点偏移。
  186. m_obOffsetInPixels = CC_POINT_POINTS_TO_PIXELS( m_obOffset );
  187. }
  188. //取得纹理块在合并时去掉周围空白边后的锚点偏移(像素单位).
  189. const CCPoint& CCSpriteFrame::getOffsetInPixels(void)
  190. {
  191. return m_obOffsetInPixels;
  192. }
  193. //设置纹理块在合并时去掉周围空白边后的锚点偏移(像素单位).
  194. void CCSpriteFrame::setOffsetInPixels(const CCPoint& offsetInPixels)
  195. {
  196. //保存像素单位的纹理块在合并时去掉周围空白边后的锚点偏移。
  197.   m_obOffsetInPixels = offsetInPixels;
  198.   //保存转换为点单位的纹理块在合并时去掉周围空白边后的锚点偏移.
  199. m_obOffset = CC_POINT_PIXELS_TO_POINTS( m_obOffsetInPixels );
  200. }
  201. //设置图集纹理
  202. void CCSpriteFrame::setTexture(CCTexture2D * texture)
  203. {
  204. //删放旧图集纹理,使用新图集纹理。
  205. if( m_pobTexture != texture ) {
  206. CC_SAFE_RELEASE(m_pobTexture);
  207. //对应CC_SAFE_RELEASE对引用计数器减一操作, CC_SAFE_RETAIN对引用计数器加一操作.
  208. CC_SAFE_RETAIN(texture);
  209. m_pobTexture = texture;
  210. }
  211. }
  212. //取得图集纹理。
  213. CCTexture2D* CCSpriteFrame::getTexture(void)
  214. {
  215. //如果纹理指针有效,直接返回。
  216. if( m_pobTexture ) {
  217. return m_pobTexture;
  218. }
  219. //否则查看纹理图片是否有效,如果有效,加载到纹理管理器中返回创建的纹理。
  220. if( m_strTextureFilename.length() > ) {
  221. return CCTextureCache::sharedTextureCache()->addImage(m_strTextureFilename.c_str());
  222. }
  223. // 如果都无效,返回NULL。
  224. return NULL;
  225. }
  226.  
  227. NS_CC_END

现在来看CCAnimationFrame 和CCAnimation:

  1. #ifndef __CC_ANIMATION_H__
  2. #define __CC_ANIMATION_H__
  3. //引入相关头文件。
  4. #include "platform/CCPlatformConfig.h"
  5. #include "cocoa/CCObject.h"
  6. #include "cocoa/CCArray.h"
  7. #include "cocoa/CCDictionary.h"
  8. #include "cocoa/CCGeometry.h"
  9. #include "CCSpriteFrame.h"
  10. #include <string>
  11. //使用Cocos2d命名空间。
  12. NS_CC_BEGIN
  13. //用到的类。
  14. class CCTexture2D;
  15. class CCSpriteFrame;
  16.  
  17. //序列帧动画单帧:序列帧动画中的一帧。
  18. class CC_DLL CCAnimationFrame : public CCObject
  19. {
  20. public:
  21. //构造
  22. CCAnimationFrame();
  23. //析构
  24. virtual ~CCAnimationFrame();
  25. //创建一个当前实例的拷贝。
  26. virtual CCObject* copyWithZone(CCZone* pZone);
  27. //初始化单帧:参一为精灵帧,参二为当前帧到下一帧的时间间隔?错,这里有问题,后面讲,参三为存储各帧所用的词典。
  28. bool initWithSpriteFrame(CCSpriteFrame* spriteFrame, float delayUnits, CCDictionary* userInfo);
  29.  
  30. //定义一个精灵帧成员指针变量m_pSpriteFrame,代表当前动画帧所对应的精灵帧。CC_SYNTHESIZE_RETAIN宏创建对变量的set 和 get 操作的函数,函数名分别为setSpriteFrame和getSpriteFrame, 相应的set函数会释放旧的指针变量,并将参数保存到指针变量,并自动的对其引用计数器加一。
  31. CC_SYNTHESIZE_RETAIN(CCSpriteFrame*, m_pSpriteFrame, SpriteFrame)
  32.  
  33. //定义一个float成员变量m_fDelayUnits,代表当前帧到下一帧的时间间隔?错,这个变量其实没有毛用处?看多了反而容易乱,先略过吧。最后统一分析。CC_SYNTHESIZE宏创建对变量的set 和 get 操作的函数,函数名分别为setDelayUnits和getDelayUnits。
  34. CC_SYNTHESIZE(float, m_fDelayUnits, DelayUnits)
  35.  
  36. //定义一个词典成员指针变量m_pUserInfo,CC_SYNTHESIZE_RETAIN宏创建对变量的set 和 get 操作的函数,函数名分别为set UserInfo和get UserInfo, 相应的set函数会释放旧的指针变量,并将参数保存到指针变量,并自动的对其引用计数器加一。
  37. CC_SYNTHESIZE_RETAIN(CCDictionary*, m_pUserInfo, UserInfo)
  38. };
  39.  
  40. //CCAnimation:序列帧动画类,管理所有动画中的CCAnimationFrame。
  41. class CC_DLL CCAnimation : public CCObject
  42. {
  43. public:
  44. //构造函数。
  45. CCAnimation();
  46. //析构函数。
  47. ~CCAnimation(void);
  48. public:
  49. //创建一个序列帧动画,内部调用create实现。
  50. CC_DEPRECATED_ATTRIBUTE static CCAnimation* animation(void);
  51.  
  52. //创建一个序列帧动画,内部调用createWithSpriteFrames实现。参数一为存储单帧的数组,参数二为动画的每一帧与下帧的时间间隔。
  53. CC_DEPRECATED_ATTRIBUTE static CCAnimation* animationWithSpriteFrames(CCArray* arrayOfSpriteFrameNames, float delay = 0.0f);
  54.  
  55. //创建一个序列帧动画,内部调用相应create实现。参数一为存储单帧的数组,参数二为动画的每一帧与下帧的时间间隔,参三为循环次数。
  56. CC_DEPRECATED_ATTRIBUTE static CCAnimation* animationWithAnimationFrames(CCArray *arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops);
  57.  
  58. //创建一个序列帧动画。
  59. static CCAnimation* create(void);
  60.  
  61. //创建一个序列帧动画,参数一为存储单帧的数组,参数二为动画的每一帧与下帧的时间间隔。
  62. static CCAnimation* createWithSpriteFrames(CCArray* arrayOfSpriteFrameNames, float delay = 0.0f);
  63.  
  64. //创建一个序列帧动画。参数一为存储单帧的数组,参数二为动画的每一帧与下帧的时间间隔,参三为循环次数。
  65. static CCAnimation* create(CCArray *arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops);
  66.  
  67. //新加一帧到序列帧动画中,参数为CCSpriteFrame指针。
  68. void addSpriteFrame(CCSpriteFrame *pFrame);
  69.  
  70. //新加一帧加入到序列帧动画中,参数为单帧图片名称。
  71. void addSpriteFrameWithFileName(const char *pszFileName);
  72.  
  73. //新加一帧加入到序列帧动画中,参一为图集纹理,参二为图集纹理中的图块矩形。
  74. void addSpriteFrameWithTexture(CCTexture2D* pobTexture, const CCRect& rect);
  75.  
  76. //初始化一个空白的动画。
  77. bool init();
  78.  
  79. //初始化序列帧动画,参数一为存储单帧的数组,参数二为动画的每一帧与下帧的时间间隔。
  80. bool initWithSpriteFrames(CCArray *pFrames, float delay = 0.0f);
  81.  
  82. //初始化序列帧动画,参数一为存储单帧的数组,参数二为动画的每一帧与下帧的时间间隔,参三为循环次数。
  83. bool initWithAnimationFrames(CCArray* arrayOfAnimationFrames, float delayPerUnit, unsigned int loops);
  84.  
  85. //创建一个当前类的实例对象。
  86. virtual CCObject* copyWithZone(CCZone* pZone);
  87.  
  88. //定义一个float成员变量m_ fTotalDelayUnits,代表总帧数。CC_SYNTHESIZE_READONLY宏创建对变量的get 操作的函数,函数为getTotalDelayUnits。
  89. CC_SYNTHESIZE_READONLY(float, m_fTotalDelayUnits, TotalDelayUnits)
  90.  
  91. //定义一个float成员变量m_fDelayPerUnit,代表每两帧间的时间间隔,CC_SYNTHESIZE宏创建对变量的set 和 get 操作的函数,函数名分别为setDelayPerUnit和getDelayPerUnit。
  92. CC_SYNTHESIZE(float, m_fDelayPerUnit, DelayPerUnit)
  93.  
  94. //定义一个float成员变量m_fDuration,这个变量没用,不知作者为什么要加这个变量~。CC_SYNTHESIZE_READONLY宏创建对变量的get 操作的函数,函数为getDuration。
  95. CC_PROPERTY_READONLY(float, m_fDuration, Duration)
  96.  
  97. //定义一个词典成员指针变量m_pFrames,用于保存每个动画帧,CC_SYNTHESIZE_RETAIN宏创建对变量的set 和 get 操作的函数,函数名分别为setFrames和getFrames, 相应的set函数会释放旧的指针变量,并将参数保存到指针变量,并自动的对其引用计数器加一。
  98. CC_SYNTHESIZE_RETAIN(CCArray*, m_pFrames, Frames)
  99.  
  100. //定义一个bool成员变量m_bRestoreOriginalFrame,用于指定是否在动画结束后释放各帧。CC_SYNTHESIZE宏创建对变量的set 和 get 操作的函数,函数名分别为setRestoreOriginalFrame和getRestoreOriginalFrame。
  101. CC_SYNTHESIZE(bool, m_bRestoreOriginalFrame, RestoreOriginalFrame)
  102.  
  103. //定义一个unsigned int成员变量m_uLoops,代表循环播放次数。CC_SYNTHESIZE宏创建对变量的set 和 get 操作的函数,函数名分别为setLoops和getLoops。
  104. CC_SYNTHESIZE(unsigned int, m_uLoops, Loops)
  105. };
  106.  
  107. NS_CC_END
  108.  
  109. #endif // __CC_ANIMATION_H__

再看CPP:

  1. #include "CCAnimation.h"
  2. #include "textures/CCTextureCache.h"
  3. #include "textures/CCTexture2D.h"
  4. #include "ccMacros.h"
  5. #include "sprite_nodes/CCSpriteFrame.h"
  6. #include "cocoa/CCZone.h"
  7. //Cocos2d命名空间
  8. NS_CC_BEGIN
  9. //构造
  10. CCAnimationFrame::CCAnimationFrame()
  11. : m_pSpriteFrame(NULL)
  12. , m_fDelayUnits(0.0f)
  13. , m_pUserInfo(NULL)
  14. {
  15.  
  16. }
  17. //初始化单帧:参一为精灵帧,参二为当前帧到下一帧的时间间隔,参三为存储各帧所用的词典。
  18. bool CCAnimationFrame::initWithSpriteFrame(CCSpriteFrame* spriteFrame, float delayUnits, CCDictionary* userInfo)
  19. {
  20. //设置为当前单帧所用的各成员变量。
  21. setSpriteFrame(spriteFrame);
  22. setDelayUnits(delayUnits);
  23. setUserInfo(userInfo);
  24.  
  25. return true;
  26. }
  27. //析构
  28. CCAnimationFrame::~CCAnimationFrame()
  29. {
  30. CCLOGINFO( "cocos2d: deallocing %s", this);
  31. //对占用的精灵帧和词典引用计数估减一操作。
  32. CC_SAFE_RELEASE(m_pSpriteFrame);
  33. CC_SAFE_RELEASE(m_pUserInfo);
  34. }
  35. //创建当前单帧的实例拷贝。
  36. CCObject* CCAnimationFrame::copyWithZone(CCZone* pZone)
  37. {
  38. //定义拷贝类变量指针
  39. CCZone* pNewZone = NULL;
  40. //定义单帧类指针变量pCopy用于存储创建结果,置空。
  41. CCAnimationFrame* pCopy = NULL;
  42. //如果参数pZone有效且已经有内部产生的实例拷贝。
  43. if(pZone && pZone->m_pCopyObject)
  44. {
  45. //直接取得拷贝返回给pCopy.
  46. pCopy = (CCAnimationFrame*)(pZone->m_pCopyObject);
  47. }
  48. else
  49. {
  50. //如果参数pZone无值或者无内部产生的拷贝,重新生成单帧以及相应单帧的拷贝。
  51. pCopy = new CCAnimationFrame();
  52. pNewZone = new CCZone(pCopy);
  53. }
  54. //对pCopy进行初始化,注意相应成员变量也要调用copy以产生拷贝,并调用autorelease以交由内存管理器进行释放管理。
  55.  
  56. pCopy->initWithSpriteFrame((CCSpriteFrame*)m_pSpriteFrame->copy()->autorelease(),
  57. m_fDelayUnits, m_pUserInfo != NULL ? (CCDictionary*)m_pUserInfo->copy()->autorelease() : NULL);
  58. //释放pNewZone
  59. CC_SAFE_DELETE(pNewZone);
  60. //返回pCopy。
  61. return pCopy;
  62. }
  63.  
  64. //创建动画
  65. CCAnimation* CCAnimation::animation(void)
  66. {
  67. return CCAnimation::create();
  68. }
  69. //创建动画。
  70. CCAnimation* CCAnimation::create(void)
  71. {
  72. //使用new创建一个动画,初始化并交由内存管理器进行释放管理。
  73. CCAnimation *pAnimation = new CCAnimation();
  74. pAnimation->init();
  75. pAnimation->autorelease();
  76. //返回创建成功的动画。
  77. return pAnimation;
  78. }
  79. //创建一个序列帧动画,内部调用相应createWithSpriteFrames实现。参数一为存储单帧的数组,参数二为动画的每一帧与下帧的时间间隔。
  80. CCAnimation* CCAnimation::animationWithSpriteFrames(CCArray *frames, float delay/* = 0.0f*/)
  81. {
  82. return CCAnimation::createWithSpriteFrames(frames, delay);
  83. }
  84. //创建一个序列帧动画。参数一为存储单帧的数组,参数二为动画的每一帧与下帧的时间间隔。
  85.  
  86. CCAnimation* CCAnimation::createWithSpriteFrames(CCArray *frames, float delay/* = 0.0f*/)
  87. {
  88. //使用new创建一个动画实例,初始化后交由内存管理器进行释放管理。
  89. CCAnimation *pAnimation = new CCAnimation();
  90. pAnimation->initWithSpriteFrames(frames, delay);
  91. pAnimation->autorelease();
  92. //返回创建的动画实例。
  93. return pAnimation;
  94. }
  95. //创建一个序列帧动画,内部调用相应create实现。参数一为存储单帧的数组,参数二为动画的每一帧与下帧的时间间隔,参三为循环次数。
  96.  
  97. CCAnimation* CCAnimation::animationWithAnimationFrames(CCArray* arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops)
  98. {
  99. return CCAnimation::create(arrayOfAnimationFrameNames, delayPerUnit, loops);
  100. }
  101. //创建一个序列帧动画,参数一为存储单帧的数组,参数二为动画的每一帧与下帧的时间间隔,参三为循环次数。
  102.  
  103. CCAnimation* CCAnimation::create(CCArray* arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops)
  104. {
  105. //使用new创建一个动画实例,初始化后交由内存管理器进行释放管理。
  106. CCAnimation *pAnimation = new CCAnimation();
  107. pAnimation->initWithAnimationFrames(arrayOfAnimationFrameNames, delayPerUnit, loops);
  108. pAnimation->autorelease();
  109. //返回创建的动画实例。
  110. return pAnimation;
  111. }
  112. //初始化一个空白的动画。
  113. bool CCAnimation::init()
  114. {
  115. return initWithSpriteFrames(NULL, 0.0f);
  116. }
  117. //初始化序列帧动画,参数一为存储精灵帧的数组,参数二为动画的每一帧与下帧的时间间隔。
  118. bool CCAnimation::initWithSpriteFrames(CCArray *pFrames, float delay/* = 0.0f*/)
  119. {
  120. //对pFrames是否是CCSpriteFrame指针数组进行检查。
  121. CCARRAY_VERIFY_TYPE(pFrames, CCSpriteFrame*);
  122. //将循环次数置1.
  123. m_uLoops = ;
  124. //保存每一帧与下一帧的时间间隔。
  125. m_fDelayPerUnit = delay;
  126. //创建一个CArray,返回它的地址给指针pTmpFrames。
  127. CCArray* pTmpFrames = CCArray::create();
  128. //将pTmpFrames设置为当前动画的帧数组指针。
  129. setFrames(pTmpFrames);
  130. //如果代表单帧数组的参数pFrames有值,遍历各帧存入pTmpFrames.
  131. if (pFrames != NULL)
  132. {
  133. //定义临时变量pObj用来存储遍历时的元素。
  134. CCObject* pObj = NULL;
  135. //遍历pFrames,以pObj为每次循环的元素指针。
  136. CCARRAY_FOREACH(pFrames, pObj)
  137. {
  138. //将pObj转为精灵帧指针。
  139. CCSpriteFrame* frame = (CCSpriteFrame*)pObj;
  140. //创建一个动画单帧,并将精灵单帧做为参数对其进行初始化。
  141. CCAnimationFrame *animFrame = new CCAnimationFrame();
  142. animFrame->initWithSpriteFrame(frame, , NULL);
  143. //将初始化完成后的动画单帧放入动画帧容器。
  144. m_pFrames->addObject(animFrame);
  145. //对动画单帧的引用计数器减1,不会被释放的,因为上面一行代码中addObject对它的引用计数器内部会做加1操作。这样“该接手的加1,该放手的减1”完成了交接工作。
  146. animFrame->release();
  147. //动画帧数加一。
  148. m_fTotalDelayUnits++;
  149. }
  150. }
  151.  
  152. return true;
  153. }
  154. //初始化序列帧动画,参数一为存储单帧的数组,参数二为动画的每一帧与下帧的时间间隔,参数三为循环播放次数。
  155. bool CCAnimation::initWithAnimationFrames(CCArray* arrayOfAnimationFrames, float delayPerUnit, unsigned int loops)
  156. {
  157. //对pFrames是否是CCSpriteFrame指针数组进行检查。
  158. CCARRAY_VERIFY_TYPE(arrayOfAnimationFrames, CCAnimationFrame*);
  159. //保存每一帧与下一帧的时间间隔。
  160. m_fDelayPerUnit = delayPerUnit;
  161. //保存循环次数
  162. m_uLoops = loops;
  163. //将arrayOfAnimationFrames设置为当前动画的帧数组指针。
  164. setFrames(CCArray::createWithArray(arrayOfAnimationFrames));
  165. //定义临时变量pObj用来存储遍历时的元素。
  166. CCObject* pObj = NULL;
  167. //遍历pFrames,以pObj为每次循环的元素指针。
  168. CCARRAY_FOREACH(m_pFrames, pObj)
  169. {
  170. //将pObj转为动画单帧指针。
  171. CCAnimationFrame* animFrame = (CCAnimationFrame*)pObj;
  172. //这里写的有误,应该改成动画帧数加一,它加动画帧的相应函数干毛?
  173. m_fTotalDelayUnits += animFrame->getDelayUnits();
  174. }
  175. //返回成功。
  176. return true;
  177. }
  178. //构造
  179. CCAnimation::CCAnimation()
  180. : m_fTotalDelayUnits(0.0f)
  181. , m_fDelayPerUnit(0.0f)
  182. , m_fDuration(0.0f)
  183. , m_pFrames(NULL)
  184. , m_bRestoreOriginalFrame(false)
  185. , m_uLoops()
  186. {
  187.  
  188. }
  189. //析构
  190. CCAnimation::~CCAnimation(void)
  191. {
  192. CCLOGINFO("cocos2d, deallocing %p", this);
  193. CC_SAFE_RELEASE(m_pFrames);
  194. }
  195.  
  196. //新加一帧到序列帧动画中,参数为CCSpriteFrame指针。&nbsp;
  197. void CCAnimation::addSpriteFrame(CCSpriteFrame *pFrame)
  198. {
  199. //利用new新建一个动画帧.
  200. CCAnimationFrame *animFrame = new CCAnimationFrame();
  201. //使用精灵指参数对其进行初始化。
  202. animFrame->initWithSpriteFrame(pFrame, 1.0f, NULL);
  203. //将生成的动画帧放入动画帧容器
  204. m_pFrames->addObject(animFrame);
  205. //对动画单帧的引用计数器减1。(新建的animFrame,交给m_pFrames的addObject函数会对其引用计数器加1,后就要减1才对。交接也要保证引用计数的平衡)
  206. animFrame->release();
  207. //动画帧数加一。
  208. m_fTotalDelayUnits++;
  209. }
  210. //新加一帧加入到序列帧动画中,参数为单帧图片名称。
  211. void CCAnimation::addSpriteFrameWithFileName(const char *pszFileName)
  212. {
  213. //先由图片名称取得创建的纹理。
  214. CCTexture2D *pTexture = CCTextureCache::sharedTextureCache()->addImage(pszFileName);
  215. //定义临时矩形保存图片大小。
  216. CCRect rect = CCRectZero;
  217. rect.size = pTexture->getContentSize();
  218. //由当前图片和对应大小创建出一个精灵帧 。
  219. CCSpriteFrame *pFrame = CCSpriteFrame::createWithTexture(pTexture, rect);
  220. //使用创建的精灵帧来调用上一个函数增加一帧动画。
  221. addSpriteFrame(pFrame);
  222. }
  223. //新加一帧加入到序列帧动画中,参数一为图集纹理,参数二为对应的纹理块矩形。
  224. void CCAnimation::addSpriteFrameWithTexture(CCTexture2D *pobTexture, const CCRect& rect)
  225. {
  226. //由当前图片和对应大小创建出一个精灵帧 。
  227. CCSpriteFrame *pFrame = CCSpriteFrame::createWithTexture(pobTexture, rect);
  228. //使用创建的精灵帧来增加一帧动画。
  229. addSpriteFrame(pFrame);
  230. }
  231. //取得序列帧动画总时长。
  232. float CCAnimation::getDuration(void)
  233. {
  234. //总时长 = 帧数x帧间隔时间。
  235. return m_fTotalDelayUnits * m_fDelayPerUnit;
  236. }
  237. //创建当前序列帧动画实例的拷贝。
  238. CCObject* CCAnimation::copyWithZone(CCZone* pZone)
  239. {
  240. //定义拷贝类变量指针
  241. CCZone* pNewZone = NULL;
  242. //定义序列帧动画类指针变量pCopy用于存储创建结果,置空。
  243. CCAnimation* pCopy = NULL;
  244. //如果参数pZone有效且已经有内部产生的实例拷贝。
  245.  
  246. if(pZone && pZone->m_pCopyObject)
  247. {
  248. //直接取得拷贝返回给pCopy.
  249. pCopy = (CCAnimation*)(pZone->m_pCopyObject);
  250. }
  251. else
  252. {
  253. //如果参数pZone无值或者无内部产生的拷贝,重新生成单帧以及相应单帧的拷贝。
  254. pCopy = new CCAnimation();
  255. pNewZone = new CCZone(pCopy);
  256. }
  257. //初始化序列序动画。
  258. pCopy->initWithAnimationFrames(m_pFrames, m_fDelayPerUnit, m_uLoops);
  259. //设置相应变理。
  260. pCopy->setRestoreOriginalFrame(m_bRestoreOriginalFrame);
  261. //释放pNewZone.
  262. CC_SAFE_DELETE(pNewZone);
  263. return pCopy;
  264. }
  265.  
  266. NS_CC_END

最后我们看一下CCAnimate:

  1. class CCTexture2D;
  2. //序列帧动画
  3. class CC_DLL CCAnimate : public CCActionInterval
  4. {
  5. public:
  6. //构造与析构。
  7. CCAnimate();
  8. ~CCAnimate();
  9.  
  10. //初始化动画。
  11. bool initWithAnimation(CCAnimation *pAnimation);
  12. //重载基类的相应函数。
  13. virtual CCObject* copyWithZone(CCZone* pZone);
  14. virtual void startWithTarget(CCNode *pTarget);
  15. virtual void stop(void);
  16. virtual void update(float t);
  17. virtual CCActionInterval* reverse(void);
  18.  
  19. public:
  20. //创建一个序列帧动画,内部调用create实现。参数为序列帧动画信息结构指针。
  21. CC_DEPRECATED_ATTRIBUTE static CCAnimate* actionWithAnimation(CCAnimation *pAnimation);
  22. //创建一个序列帧动画。
  23. static CCAnimate* create(CCAnimation *pAnimation);
  24. //定义一个序列帧动画信息结构指针变量以及存取此变量的函数。
  25. CC_SYNTHESIZE_RETAIN(CCAnimation*, m_pAnimation, Animation)
  26. protected:
  27. //保存每一帧要切换时的进度的容器指针,由动画信息结构指针取得。
  28. std::vector<float>* m_pSplitTimes;
  29. //当前要播放的下一帧序号。
  30. int m_nNextFrame;
  31. //各帧中保存的精灵信息。
  32. CCSpriteFrame* m_pOrigFrame;
  33. //循环次数。
  34. unsigned int m_uExecutedLoops;
  35. };
  36. 具体实现:
  37. //创建一个序列帧动画,内部调用create实现。参数为动画信息结构。
  38. CCAnimate* CCAnimate::actionWithAnimation(CCAnimation *pAnimation)
  39. {
  40. return CCAnimate::create(pAnimation);
  41. }
  42. //创建一个序列帧动画。
  43. CCAnimate* CCAnimate::create(CCAnimation *pAnimation)
  44. {
  45. //使用new创建一个CCAnimate实例对象,初始化,交由内存管理器。
  46. CCAnimate *pAnimate = new CCAnimate();
  47. pAnimate->initWithAnimation(pAnimation);
  48. pAnimate->autorelease();
  49. //返回创建的实例对象。
  50. return pAnimate;
  51. }
  52. //初始化序列动画。
  53. bool CCAnimate::initWithAnimation(CCAnimation *pAnimation)
  54. {
  55. //有效性判断。
  56. CCAssert( pAnimation!=NULL, "Animate: argument Animation must be non-NULL");
  57. //取得序列的时长。
  58. float singleDuration = pAnimation->getDuration();
  59. //乘以循环次数做为当前动画总时长来进行初始化。
  60. if ( CCActionInterval::initWithDuration(singleDuration * pAnimation->getLoops() ) )
  61. {
  62. //初始化变量。
  63. m_nNextFrame = ;
  64. //将参数pAnimation保存到动画信息结构指针变量m_pAnimation.
  65. setAnimation(pAnimation);
  66. m_pOrigFrame = NULL;
  67. m_uExecutedLoops = ;
  68. //设置容器大小。这里我认为应该写成resize而不是reserver!!!
  69. m_pSplitTimes->reserve(pAnimation->getFrames()->count());
  70. //初始化变量。
  71. float accumUnitsOfTime = ;
  72. //序列播放一轮的时间/
  73. float newUnitOfTimeValue = singleDuration / pAnimation->getTotalDelayUnits();
  74. //取得序列信息中的帧信息数组。
  75. CCArray* pFrames = pAnimation->getFrames();
  76. //判断pFrames是否是CCAnimationFrame指针类型,确保是。
  77. CCARRAY_VERIFY_TYPE(pFrames, CCAnimationFrame*);
  78. //定义临时变量pObj来遍历取得帧信息中的单帧信息。
  79. CCObject* pObj = NULL;
  80. CCARRAY_FOREACH(pFrames, pObj)
  81. {
  82. //取得单帧信息。
  83. CCAnimationFrame* frame = (CCAnimationFrame*)pObj;
  84. //计算播放当前单帧时的进度,这里又用到了单帧信息的接口getDelayUnits。后面讲一下。
  85. float value = (accumUnitsOfTime * newUnitOfTimeValue) / singleDuration;
  86. accumUnitsOfTime += frame->getDelayUnits();
  87. //将当前单帧的进度存入容器。
  88. m_pSplitTimes->push_back(value);
  89. }
  90. //返回成功。
  91. return true;
  92. }
  93. //如果初始化失败,返回false。
  94. return false;
  95. }
  96. //创建拷贝。
  97. CCObject* CCAnimate::copyWithZone(CCZone *pZone)
  98. {
  99. CCZone* pNewZone = NULL;
  100. CCAnimate* pCopy = NULL;
  101. if(pZone && pZone->m_pCopyObject)
  102. {
  103. //in case of being called at sub class
  104. pCopy = (CCAnimate*)(pZone->m_pCopyObject);
  105. }
  106. else
  107. {
  108. pCopy = new CCAnimate();
  109. pZone = pNewZone = new CCZone(pCopy);
  110. }
  111.  
  112. CCActionInterval::copyWithZone(pZone);
  113.  
  114. pCopy->initWithAnimation((CCAnimation*)m_pAnimation->copy()->autorelease());
  115.  
  116. CC_SAFE_DELETE(pNewZone);
  117. return pCopy;
  118. }
  119. //构造,注意在这里申请了一个vector<float>容器,并将成员指针变量m_pSplitTimes指向它的。
  120. CCAnimate::CCAnimate()
  121. : m_pAnimation(NULL)
  122. , m_pSplitTimes(new std::vector<float>)
  123. , m_nNextFrame()
  124. , m_pOrigFrame(NULL)
  125. , m_uExecutedLoops()
  126. {
  127.  
  128. }
  129. //析构。
  130. CCAnimate::~CCAnimate()
  131. {
  132. CC_SAFE_RELEASE(m_pAnimation);
  133. CC_SAFE_RELEASE(m_pOrigFrame);
  134. CC_SAFE_DELETE(m_pSplitTimes);
  135. }
  136. //设置演示当前序列动画的演员。
  137. void CCAnimate::startWithTarget(CCNode *pTarget)
  138. {
  139. //先调用基类的相应函数。
  140. CCActionInterval::startWithTarget(pTarget);
  141. //序列帧动画必须是个精灵。
  142. CCSprite *pSprite = (CCSprite*)(pTarget);
  143. //释放上一个
  144. CC_SAFE_RELEASE(m_pOrigFrame);
  145. //如果有帧数据。
  146. if (m_pAnimation->getRestoreOriginalFrame())
  147. {
  148. //取得精灵的帧信息。
  149. m_pOrigFrame = pSprite->displayFrame();
  150. //对帧信息占用,引用数加一。
  151. m_pOrigFrame->retain();
  152. }
  153. m_nNextFrame = ;
  154. m_uExecutedLoops = ;
  155. }
  156. //停止当前动画。
  157. void CCAnimate::stop(void)
  158. {
  159. //如果动画有帧数据且有演员。
  160. if (m_pAnimation->getRestoreOriginalFrame() && m_pTarget)
  161. {
  162. //设置演员显示当前帧。
  163. ((CCSprite*)(m_pTarget))->setDisplayFrame(m_pOrigFrame);
  164. }
  165. //停止当前动画。
  166. CCActionInterval::stop();
  167. }
  168. //更新动画。
  169. void CCAnimate::update(float t)
  170. {
  171. // 如果整个动画未播放,先计算循环是否够次数。
  172. if( t < 1.0f ) {
  173. //计算出当前进度播放的循环数。
  174. t *= m_pAnimation->getLoops();
  175.  
  176. // 通过先取整再判断是否大于当前的已经循环次数来判断是否是新的循环,如果是将下一帧置零,已经循环的次数加1 。
  177. unsigned int loopNumber = (unsigned int)t;
  178. if( loopNumber > m_uExecutedLoops ) {
  179. m_nNextFrame = ;
  180. m_uExecutedLoops++;
  181. }
  182.  
  183. // 对t进行浮点取模值,将其限制在0~1之间,这样等于又转换成为了当前动画播放的进度值。
  184. t = fmodf(t, 1.0f);
  185. }
  186. //取得动画的帧信息容器和帧数。
  187. CCArray* frames = m_pAnimation->getFrames();
  188. unsigned int numberOfFrames = frames->count();
  189. //精灵图片信息。
  190. CCSpriteFrame *frameToDisplay = NULL;
  191. //找出要播放的帧图片设置为精灵要显示的图片,这里用了一个for循环。从下一帧开始到结束帧进行遍历,判断是否到了这一帧。
  192. for( unsigned int i=m_nNextFrame; i < numberOfFrames; i++ ) {
  193. //取出循环中的当前帧的播出时间进度。
  194. float splitTime = m_pSplitTimes->at(i);
  195. //如果这一帧的进度小于当前动画的播放进度,即代表进入了这一帧。
  196. if( splitTime <= t ) {
  197. //取得对应的动画帧信息。
  198. CCAnimationFrame* frame = (CCAnimationFrame*)frames->objectAtIndex(i);
  199. //取得当前帧的精灵图片信息。
  200. frameToDisplay = frame->getSpriteFrame();
  201. //这才是显示动画的关键,找到相应的精灵帧并设置为演员要显示的当前帧。
  202. ((CCSprite*)m_pTarget)->setDisplayFrame(frameToDisplay);
  203. //通过动画帧信息取得其附加的用户词典信息,这个词典存储的是用于需要通知的目标。
  204. CCDictionary* dict = frame->getUserInfo();
  205. if( dict )
  206. {
  207. //暂忽略了。
  208. //TODO: [[NSNotificationCenter defaultCenter] postNotificationName:CCAnimationFrameDisplayedNotification object:target_ userInfo:dict];
  209. }
  210. //帧数加一。
  211. m_nNextFrame = i+;
  212.  
  213. break;
  214. }
  215. }
  216. }
  217. //创建一个反向播放的序列帧动画。
  218. CCActionInterval* CCAnimate::reverse(void)
  219. {
  220. CCArray* pOldArray = m_pAnimation->getFrames();
  221. CCArray* pNewArray = CCArray::createWithCapacity(pOldArray->count());
  222.  
  223. CCARRAY_VERIFY_TYPE(pOldArray, CCAnimationFrame*);
  224.  
  225. if (pOldArray->count() > )
  226. {
  227. CCObject* pObj = NULL;
  228. CCARRAY_FOREACH_REVERSE(pOldArray, pObj)
  229. {
  230. CCAnimationFrame* pElement = (CCAnimationFrame*)pObj;
  231. if (! pElement)
  232. {
  233. break;
  234. }
  235.  
  236. pNewArray->addObject((CCAnimationFrame*)(pElement->copy()->autorelease()));
  237. }
  238. }
  239.  
  240. CCAnimation *newAnim = CCAnimation::create(pNewArray, m_pAnimation->getDelayPerUnit(), m_pAnimation->getLoops());
  241. newAnim->setRestoreOriginalFrame(m_pAnimation->getRestoreOriginalFrame());
  242. return create(newAnim);
  243. }

源码看完了,来看一个小示例 ,以ActionsTest为例,我将ActionAnimate做了稍许修改,只有一个演员演示一个14帧的序列帧动画。我们来看一下实际的动作生成过程。

  1. void ActionAnimate::onEnter()
  2. {
  3. ActionsDemo::onEnter();
  4. //创建一个演员,站在屏幕中央。
  5. centerSprites();
  6. //创建一个空白的序列帧动画信息。
  7. CCAnimation* animation = CCAnimation::create();
  8. //共有14帧,这里用for循环将对应的序列图加入到动画中。
  9. for( int i=;i<;i++)
  10. {
  11. char szName[] = {};
  12. sprintf(szName, "Images/grossini_dance_%02d.png", i);
  13. animation->addSpriteFrameWithFileName(szName);
  14. }
  15. //设置每两帧间时间间隔为1秒。
  16. animation->setDelayPerUnit(1.0f);
  17. //设置动画结束后仍保留动画帧信息。
  18. animation->setRestoreOriginalFrame(true);
  19. //由这个动画信息创建一个序列帧动画。
  20. CCAnimate* action = CCAnimate::create(animation);
  21. //让演员演示这个动画。
  22. m_grossini->runAction(action);
  23. }

流程是先创建动画帧信息,然后由动画帧信息生成动画信息,最后由动画信息创建出序列帧动画供精灵演示。

这里面的关健函数是addSpriteFrameWithFileName,我们来看一下在调用它时的程序流程,当我们把图片名称赋给它后,它创建出纹理并调用createWithTexture函数来进行创建一个精灵单帧,初始化后使用这个精灵单帧信息创建一帧动画放到动画信息容器中。

  1. void CCAnimation::addSpriteFrameWithFileName(const char *pszFileName)
  2. {
  3. CCTexture2D *pTexture = CCTextureCache::sharedTextureCache()->addImage(pszFileName);
  4. CCRect rect = CCRectZero;
  5. rect.size = pTexture->getContentSize();
  6. CCSpriteFrame *pFrame = CCSpriteFrame::createWithTexture(pTexture, rect);
  7. addSpriteFrame(pFrame);
  8. }

在createWithTexture函数中,传入的是纹理和纹理大小。其内部实现了创建精灵帧并初始化的功能。

  1. CCSpriteFrame* CCSpriteFrame::createWithTexture(CCTexture2D *pobTexture, const CCRect& rect)
  2. {
  3. CCSpriteFrame *pSpriteFrame = new CCSpriteFrame();;
  4. pSpriteFrame->initWithTexture(pobTexture, rect);
  5. pSpriteFrame->autorelease();
  6.  
  7. return pSpriteFrame;
  8. }

这里的关帧是initWithTexture函数,传入的是纹理和纹理大小。

  1. bool CCSpriteFrame::initWithTexture(CCTexture2D* pobTexture, const CCRect& rect)
  2. {
  3. CCRect rectInPixels = CC_RECT_POINTS_TO_PIXELS(rect);
  4. return initWithTexture(pobTexture, rectInPixels, false, CCPointZero, rectInPixels.size);
  5. }

我们看,此函数内部以不旋转,不偏移,原始大小为当前大小等参数来进行了初始化精灵单帧的处理。这样做说明使用的就是单图集的纹理。

如果我们使用的是图集纹理,需要在调用addSpriteFrameWithFileName函数时加上相应的纹理块的相关设置参数。

最后,我来解答一下CCAnimationFrame类中的成员变量m_fDelayUnits的。这个变量其实是无用的,在整个动画处理过程中它出现过二次。一次是CCAnimation::initWithAnimationFrames函数中

  1. m_fTotalDelayUnits+= animFrame->getDelayUnits();

另一次是CCAnimate::initWithAnimation函数中在for循环中的

  1. float value = (accumUnitsOfTime* newUnitOfTimeValue) / singleDuration;
  2. accumUnitsOfTime+= frame->getDelayUnits();

先看第一次,我们知道m_fTotalDelayUnits代表的是帧的数量,其实对于单帧来说,它本身只有一帧,也就是说animFrame->getDelayUnits()的结果应该是1,所以m_fTotalDelayUnits += animFrame->getDelayUnits();应该改为m_fTotalDelayUnits ++;就好了。第二次中也一样,frame->getDelayUnits();也应该为一,因为float value =(accumUnitsOfTime * newUnitOfTimeValue) / singleDuration;这一句是用来进算当前帧在整个动画播放中的进度,float newUnitOfTimeValue = singleDuration /pAnimation->getTotalDelayUnits();这一句中singleDuration代表动画的时长,pAnimation->getTotalDelayUnits()代表总帧数,那newUnitOfTimeValue即是每两帧间的时间时间隔。既然newUnitOfTimeValue的意义是每两帧间的时间时间隔,则accumUnitsOfTime应该代表的就是当前遍历到第几帧了,它是帧索引。所以我认为CCAnimationFrame类中的成员变量m_fDelayUnits本身没有必要存在,就是1,这个变量的存在反而会影响到大家对于动画的理解,如果说CCAnimationFrame本身又支持存储多个精灵帧,那倒是需要一个变量来代表精灵帧的数量,但目前看并不是。

cocos2d-x 2.0 序列帧动画 深入分析的更多相关文章

  1. cocos2dx2.0 帧动画的创建和播放过程 深入分析

    一.帧动画的创建过程帧动画的实现有四个不可或缺的类,如下:1.CCSpriteFrame:精灵帧信息.存储帧动画的每一帧的纹理基本信息. class CC_DLL CCSpriteFrame : pu ...

  2. 时光煮雨 Unity3D实现2D人物动画① UGUI&Native2D序列帧动画

    系列目录 [Unity3D基础]让物体动起来①--基于UGUI的鼠标点击移动 [Unity3D基础]让物体动起来②--UGUI鼠标点击逐帧移动 时光煮雨 Unity3D让物体动起来③—UGUI DoT ...

  3. iOS-动画效果(首尾式动画,代码快动画,核心动画,序列帧动画)

    一.各个动画的优缺点 1.首尾动画:如果只是修改空间的属性,使用首尾动画比较方便,如果在动画结束后做后续处理,就不是那么方面了. 2.核心动画:有点在于对后续的处理方便. 3.块动画: (1)在实际的 ...

  4. 《Android开发艺术探索》读书笔记 (7) 第7章 Android动画深入分析

    本节和<Android群英传>中的第七章Android动画机制与使用技巧有关系,建议先阅读该章的总结 第7章 Android动画深入分析 7.1 View动画 (1)android动画分为 ...

  5. [Cocos2d-x v3.x]序列帧动画

      简单介绍 Cocos2d-x中.动画的详细内容是依靠精灵显示出来的,为了显示动态图片,我们须要不停切换精灵显示的内容.通过把静态的精灵变为动画播放器从而实现动画效果. 动画由帧组成,每一帧都是一个 ...

  6. Unity3d的序列帧动画

    马上这星期就要过去了,为了完成每星期写一篇博客的目标,熬夜也要写完. 最近项目中用到了很多序列帧动画,之前看教程也接触过序列帧动画,但当时没用到,就没仔细研究,这次就借着这个机会好好总结一下序列帧动画 ...

  7. (二)plist的使用和序列帧动画

    六.plist的使用方法: iOS的程序在安装在手机上以后会把全部资源文件集成在一个文件夹中,这种文件集合称为bundle,对于一般的工程,只有一个bundle,即mainbundle,因此可以通过b ...

  8. iOS开发基础-序列帧动画之Tom猫

    新建一个Single View Application,向该工程中导入Tom猫的图片资源,本示例演示Tom猫喝牛奶的动作.图片的名字为 drink_00.jpg.drink_01.jpg.....dr ...

  9. (转)NGUI系列教程七(序列帧动画UITexture 和 UIsprit)

    NGUI系列教程七(序列帧动画)   今天我给大家讲一下如何使用NGUI做序列帧动画.本节主要包括两方面内容,分别是使用UIspirit和使用UITexture 做序列帧动画.废话不说了,下面开始.还 ...

随机推荐

  1. JS异步加载的三种方式

    js加载的缺点:加载工具方法没必要阻塞文档,过得js加载会影响页面效率,一旦网速不好,那么整个网站将等待js加载而不进行后续渲染等工作. 有些工具方法需要按需加载,用到再加载,不用不加载,. 默认正常 ...

  2. Android核心分析 之二方法论探讨之概念空间篇

    方法论探讨之概念空间篇 我们潜意识就不想用计算机的方式来思考问题,我们有自己的思维描述方式,越是接近我们思维描述方式,我们越容易接受和使用.各种计算机语言,建模工具,不外乎就是建立一个更接近人的思维方 ...

  3. 在AE中通过SDE添加图层(转)

    public void AddSDELayer(bool ChkSdeLinkModle)        {            //< span style="color: #00 ...

  4. Spring之在客户端访问RESTful业务

    Spring之在客户端访问RESTful业务 RestTemplate 是客户端访问RESTful业务的核心类.在概念上与Spring其他的模板类相似,比如JdbcTemplate和JmsTempla ...

  5. FastDFS之java客户端使用

    为了方便应用程序的访问FastDFS,官网提供了fastdfs-client-java,以便更好的与应用程序结合使用. 下载fastdfs-client-java源码添加到项目工程里面,添加配置文件: ...

  6. Two Sigma OA

    刚做了两道Two Sigma OA. 还是两道老题, Friend Cycle和Longest Chain. Friend Cycle可以用Union Find来做.优化的时候因为矩阵是沿对角线对称, ...

  7. php去除数组中重复数据

    <?php /** * 去除数组中重复数据 * by www.jbxue.com **/ $input = array("a" => "green" ...

  8. 从零开始学数据分析,什么程度可以找到工作?( 内附20G、5000分钟数据分析工具教程大合集 )

    从零开始学数据分析,什么程度可以找到工作?( 内附20G.5000分钟数据分析工具教程大合集 )   我现在在Coursera上面学data science 中的R programming,过去很少接 ...

  9. 实际利率 > 名义利率

    名义利率与实际利率的关系为:实际利率=(1+名义利率/计息周期)计息周期-1. 如计息周期数=1时,两者相等.如计息周期数大于1时,实际利率大于名义利率,当计息周期数小于1时,实际利率小于名义利率. ...

  10. CSS选择符类型

    一.标签选择符:针对某一类标签,可以以标签作为选择符 <style type="text/css"> p{color:#F00; font-size:36px;} &l ...