#ifndef __CC_ANIMATION_H__
#define __CC_ANIMATION_H__
#include "platform/CCPlatformConfig.h"
#include "cocoa/CCObject.h"
#include "cocoa/CCArray.h"
#include "cocoa/CCDictionary.h"
#include "cocoa/CCGeometry.h"
#include "CCSpriteFrame.h"
#include <string>
NS_CC_BEGIN
class CCTexture2D;
class CCSpriteFrame;
/** CCAnimationFrame
A frame of the animation. It contains information like:
- sprite frame name
- # of delay units.
- offset
*/
class CC_DLL CCAnimationFrame : public CCObject
{
public:
CCAnimationFrame();
virtual ~CCAnimationFrame();
{
CCLOGINFO( "cocos2d: deallocing %s", this);
CC_SAFE_RELEASE(m_pSpriteFrame);
CC_SAFE_RELEASE(m_pUserInfo);
}
virtual CCObject* copyWithZone(CCZone* pZone);
/** initializes the animation frame with a spriteframe, number of delay units and a notification user info */
//参数二为拖延几个单位(animation有一个m_fDelayPerUnit 以此实现变速动画 。delay 则是当前帧停留) 参三为存储各帧所用的词典。
bool initWithSpriteFrame(CCSpriteFrame* spriteFrame, float delayUnits, CCDictionary* userInfo);
/** CCSpriteFrameName to be used */
//动画帧有一个精灵帧成员
CC_SYNTHESIZE_RETAIN(CCSpriteFrame*, m_pSpriteFrame, SpriteFrame)
/** how many units of time the frame takes */
CC_SYNTHESIZE(float, m_fDelayUnits, DelayUnits)
/** A CCAnimationFrameDisplayedNotification notification will be broadcast when the frame is displayed with this dictionary as UserInfo. If UserInfo is nil, then no notification will be broadcast. */
--当一个动画帧被呈现时 一个通知(关联UserInfo)将会发出
CC_SYNTHESIZE_RETAIN(CCDictionary*, m_pUserInfo, UserInfo)
//
cocos2dx中只有一个地方用到了 getUserInfo
void CCAnimate::update(float t)
{
// if t==1, ignore. Animation should finish with t==1
if( t < 1.0f ) {
t *= m_pAnimation->getLoops();
// new loop? If so, reset frame counter
unsigned int loopNumber = (unsigned int)t;
if( loopNumber > m_uExecutedLoops ) {
m_nNextFrame = 0;
m_uExecutedLoops++;
}
// new t for animations
t = fmodf(t, 1.0f);
}
CCArray* frames = m_pAnimation->getFrames();
unsigned int numberOfFrames = frames->count();
CCSpriteFrame *frameToDisplay = NULL;
for( unsigned int i=m_nNextFrame; i < numberOfFrames; i++ ) {
float splitTime = m_pSplitTimes->at(i);
if( splitTime <= t ) {
CCAnimationFrame* frame = (CCAnimationFrame*)frames->objectAtIndex(i);
frameToDisplay = frame->getSpriteFrame();
((CCSprite*)m_pTarget)->setDisplayFrame(frameToDisplay);
CCDictionary* dict = frame->getUserInfo();
if( dict ) //还注释了 所以UserInfo这个属性并未真正使用到 需要用时可以改下面代码。。。。。。
{
//TODO: [[NSNotificationCenter defaultCenter] postNotificationName:CCAnimationFrameDisplayedNotification object:target_ userInfo:dict];
}
m_nNextFrame = i+1;
}
// Issue 1438. Could be more than one frame per tick, due to low frame rate or frame delta < 1/FPS
else {
break;
}
}
}
//
};
/** A CCAnimation object is used to perform animations on the CCSprite objects.
The CCAnimation object contains CCAnimationFrame objects, and a possible delay between the frames.
You can animate a CCAnimation object by using the CCAnimate action. Example:
[sprite runAction:[CCAnimate actionWithAnimation:animation]];
*/
class CC_DLL CCAnimation : public CCObject
{
public:
CCAnimation();
~CCAnimation(void);
public:
static CCAnimation* create(void);
static CCAnimation* createWithSpriteFrames(CCArray* arrayOfSpriteFrameNames, float delay = 0.0f);
static CCAnimation* create(CCArray *arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops);
static CCAnimation* create(CCArray *arrayOfAnimationFrameNames, float delayPerUnit) {
return CCAnimation::create(arrayOfAnimationFrameNames, delayPerUnit, 1);
}
/** Adds a CCSpriteFrame to a CCAnimation.
The frame will be added with one "delay unit".
*/
void addSpriteFrame(CCSpriteFrame *pFrame);
/** Adds a frame with an image filename. Internally it will create a CCSpriteFrame and it will add it. */
void addSpriteFrameWithFileName(const char *pszFileName);
/** Adds a frame with a texture and a rect. Internally it will create a CCSpriteFrame and it will add it.
*/
void addSpriteFrameWithTexture(CCTexture2D* pobTexture, const CCRect& rect);
bool init();
bool initWithSpriteFrames(CCArray *pFrames, float delay = 0.0f);
/** Initializes a CCAnimation with CCAnimationFrame
*/
bool initWithAnimationFrames(CCArray* arrayOfAnimationFrames, float delayPerUnit, unsigned int loops);
virtual CCObject* copyWithZone(CCZone* pZone);
/** total Delay units of the CCAnimation. */
CC_SYNTHESIZE_READONLY(float, m_fTotalDelayUnits, TotalDelayUnits)
/** Delay in seconds of the "delay unit" */
CC_SYNTHESIZE(float, m_fDelayPerUnit, DelayPerUnit)
/** duration in seconds of the whole animation. It is the result of totalDelayUnits * delayPerUnit */
CC_PROPERTY_READONLY(float, m_fDuration, Duration)
/** array of CCAnimationFrames */
CC_SYNTHESIZE_RETAIN(CCArray*, m_pFrames, Frames)
/** whether or not it shall restore the original frame when the animation finishes */
CC_SYNTHESIZE(bool, m_bRestoreOriginalFrame, RestoreOriginalFrame)
/** how many times the animation is going to loop. 0 means animation is not animated. 1, animation is executed one time, ... */
CC_SYNTHESIZE(unsigned int, m_uLoops, Loops)
};
NS_CC_END
#endif // __CC_ANIMATION_H__