Cocos2d-x 2.0 序列帧动画 深入分析

Cocos2d-x 2.0 序列帧动画 深入分析

    今天,我带领大家学习一下Cocos2d-x 2.0的序列帧动画。在Cocos2d-x中,提供了相应的一些类和方法,可以方便的生成序列帧动画,这样我们就可以制做各种人物动作以及动画效果。

 

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

 

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

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

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

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

先学习一下CCSpriteFrame:

[cpp]  view plain copy
  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:  
  18.     //取得精灵帧的大小(像素单位)  
  19. inline const CCRect& getRectInPixels(void) { return m_obRectInPixels; }  
  20. //设置精灵帧的大小(像素单位)  
  21. void setRectInPixels(const CCRect& rectInPixels);  
  22.   
  23.     //取得当前帧所用的纹理块是否旋转了90度  
  24. inline bool isRotated(void) { return m_bRotated; }  
  25. //设置当前帧所用的纹理块旋转90度  
  26.     inline void setRotated(bool bRotated) { m_bRotated = bRotated; }  
  27.   
  28.     //取得当前帧所用的纹理块在图集纹理的矩形(点单位)  
  29.     inline const CCRect& getRect(void) { return m_obRect; }  
  30.     //设置当前帧所用的纹理块在图集中的矩形(点单位)  
  31.     void setRect(const CCRect& rect);  
  32.   
  33.     //取得当前帧所用的纹理块在图集中的位置(像素单位)  
  34.     const CCPoint& getOffsetInPixels(void);  
  35.     //设置当前帧所用的纹理块在图集中的位置(像素单位)  
  36.   
  37.     void setOffsetInPixels(const CCPoint& offsetInPixels);  
  38.   
  39.     //取得纹理块的原始大小(像素单位)。  
  40.     inline const CCSize& getOriginalSizeInPixels(void) { return m_obOriginalSizeInPixels; }  
  41.     //设置纹理块的原始大小(像素单位)。  
  42.     inline void setOriginalSizeInPixels(const CCSize& sizeInPixels) { m_obOriginalSizeInPixels = sizeInPixels; }  
  43.   
  44.     //取得纹理块的原始大小(点单位)。  
  45.     inline const CCSize& getOriginalSize(void) { return m_obOriginalSize; }  
  46.     //设置纹理块的原始大小(点单位)。  
  47.     inline void setOriginalSize(const CCSize& sizeInPixels) { m_obOriginalSize = sizeInPixels; }  
  48.   
  49.     //取得当前帧所使用的图集纹理  
  50.     CCTexture2D* getTexture(void);  
  51.     //设置当前帧所使用的图集纹理  
  52. void setTexture(CCTexture2D* pobTexture);  
  53.   
  54.     //取得纹理块在合并时去掉周围空白边后的锚点偏移(点单位).  
  55. const CCPoint& getOffset(void);  
  56. //设置纹理块在合并时去掉周围空白边后的锚点偏移(点单位).  
  57.     void setOffset(const CCPoint& offsets);  
  58.   
  59. public:  
  60.     //析构  
  61. ~CCSpriteFrame(void);  
  62. //创建一个当前实例的拷贝  
  63.     virtual CCObject* copyWithZone(CCZone *pZone);  
  64.   
  65.     //从一个图集纹理中创建出一帧,内部调用对应createWithTexture函数实现,参一为图集纹理,参二为纹理块矩形。  
  66.     CC_DEPRECATED_ATTRIBUTE static CCSpriteFrame* frameWithTexture(CCTexture2D* pobTexture, const CCRect& rect);  
  67.   
  68.     //从一张图集图片中创建出一帧,内部调用对应create函数实现,参一为图集图片名称,参二为纹理块矩形。  
  69.     CC_DEPRECATED_ATTRIBUTE static CCSpriteFrame* frameWithTextureFilename(const char* filename, const CCRect& rect);  
  70.   
  71.     //从一个图集纹理中创建出一帧,内部调用对应createWithTexture函数实现,参一为图集纹理,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉纹理块空白后所导致的锚点偏移,参五为纹理块的原始大小。  
  72.     CC_DEPRECATED_ATTRIBUTE static CCSpriteFrame* frameWithTexture(CCTexture2D* pobTexture, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize);  
  73.   
  74.     //从一个张图集图片中创建出一帧,内部调用对应create函数实现,参一为图集图片名称,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉纹理块空白后所导致的锚点偏移,参五为纹理块的原始大小。  
  75.     CC_DEPRECATED_ATTRIBUTE static CCSpriteFrame* frameWithTextureFilename(const char* filename, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize);  
  76.   
  77.     //从一张图片文件中以指定的矩形创建一个精灵帧。参一为图集图片名称,参二为对应纹理块的矩形。  
  78.     static CCSpriteFrame* create(const char* filename, const CCRect& rect);  
  79.       
  80.     //从一个图集图片中创建出一帧,参一为图集图片名称,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉纹理块空白后所导致的锚点偏移,参五为纹理块的原始大小。  
  81.     static CCSpriteFrame* create(const char* filename, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize);  
  82.       
  83.     //从一张图集纹理中以指定的矩形创建一个精灵帧。参一为图集纹理,参二为对应纹理块的矩形。  
  84.     static CCSpriteFrame* createWithTexture(CCTexture2D* pobTexture, const CCRect& rect);  
  85.   
  86. //从一个图集纹理中创建出一帧,参一为图集纹理,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉纹理块空白后所导致的锚点偏移,参五为纹理块的原始大小。  
  87.     static CCSpriteFrame* createWithTexture(CCTexture2D* pobTexture, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize);  
  88.   
  89. public:  
  90.     //初始化精灵帧:参一为图集纹理,参二为对应纹理块的矩形。  
  91.     bool initWithTexture(CCTexture2D* pobTexture, const CCRect& rect);  
  92.   
  93. //初始化精灵帧:参一为图集图片名称,参二为对应纹理块的矩形。  
  94.     bool initWithTextureFilename(const char* filename, const CCRect& rect);  
  95.   
  96. //初始化精灵帧:参一为图集纹理,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉纹理块空白后所导致的锚点偏移,参五为纹理块的原始大小。  
  97.   
  98.     bool initWithTexture(CCTexture2D* pobTexture, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize);  
  99.   
  100.  //初始化精灵帧:参一为图集图片名称,参二为对应纹理块的矩形,参三为矩形块是否旋转,参四为去掉纹理块空白后所导致的锚点偏移,参五为纹理块的原始大小。  
  101.     bool initWithTextureFilename(const char* filename, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize);  
  102.   
  103.   
  104. protected:  
  105.     //去掉纹理块空白后所导致的锚点偏移。  
  106. CCPoint m_obOffset;  
  107. //纹理块的原始大小(未去掉空白的大小)。  
  108. CCSize m_obOriginalSize;  
  109. //纹理块的大小(像素单位)。  
  110. CCRect m_obRectInPixels;  
  111. //矩形块是否旋转。  
  112. bool   m_bRotated;  
  113. //纹理块  
  114. CCRect m_obRect;  
  115. //去掉纹理块空白后所导致的锚点偏移(像素单位)。  
  116. CCPoint m_obOffsetInPixels;  
  117. //纹理块的原始大小(像素单位)。  
  118. CCSize m_obOriginalSizeInPixels;      
  119. //图集纹理。  
  120. CCTexture2D *m_pobTexture;  
  121. //对应的图集图片。  
  122.     std::string  m_strTextureFilename;  
  123. };  
  124.   
  125.   
  126. NS_CC_END  
  127.   
  128. #endif //__SPRITE_CCSPRITE_FRAME_H__  

再来看CCSpriteFrame.cpp:

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

现在来看CCAnimationFrame 和CCAnimation:

[cpp]  view plain copy
  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:

[cpp]  view plain copy
  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 = 1;  
  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, 1, 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(0)  
  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指针。   
  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:

[cpp]  view plain copy
  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 = 0;  
  64.          //将参数pAnimation保存到动画信息结构指针变量m_pAnimation.  
  65.         setAnimation(pAnimation);  
  66.         m_pOrigFrame = NULL;  
  67.         m_uExecutedLoops = 0;  
  68. //设置容器大小。这里我认为应该写成resize而不是reserver!!!  
  69.         m_pSplitTimes->reserve(pAnimation->getFrames()->count());  
  70.         //初始化变量。  
  71.         float accumUnitsOfTime = 0;  
  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(0)  
  124. , m_pOrigFrame(NULL)  
  125. , m_uExecutedLoops(0)  
  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 = 0;  
  154.     m_uExecutedLoops = 0;  
  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 = 0;  
  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+1;  
  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() > 0)  
  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帧的序列帧动画。我们来看一下实际的动作生成过程。

[cpp]  view plain copy
  1. void ActionAnimate::onEnter()  
  2. {  
  3.     ActionsDemo::onEnter();  
  4.     //创建一个演员,站在屏幕中央。  
  5.     centerSprites(1);  
  6.     //创建一个空白的序列帧动画信息。  
  7.     CCAnimation* animation = CCAnimation::create();  
  8.     //共有14帧,这里用for循环将对应的序列图加入到动画中。  
  9.     forint i=1;i<15;i++)  
  10.     {  
  11.         char szName[100] = {0};  
  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函数来进行创建一个精灵单帧,初始化后使用这个精灵单帧信息创建一帧动画放到动画信息容器中。

[cpp]  view plain copy
  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函数中,传入的是纹理和纹理大小。其内部实现了创建精灵帧并初始化的功能。

[cpp]  view plain copy
  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函数,传入的是纹理和纹理大小。

[cpp]  view plain copy
  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函数中

m_fTotalDelayUnits+= animFrame->getDelayUnits();

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

 

    float value = (accumUnitsOfTime* newUnitOfTimeValue) / singleDuration;

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本身又支持存储多个精灵帧,那倒是需要一个变量来代表精灵帧的数量,但目前看并不是

原文:http://blog.csdn.net/honghaier/article/details/8222401

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值