#ifndef __CC_ANIMATION_CACHE_H__
#define __CC_ANIMATION_CACHE_H__
#include "cocoa/CCObject.h"
#include "cocoa/CCDictionary.h"
#include <string>
NS_CC_BEGIN
class CCAnimation;
/**
* @addtogroup sprite_nodes
* @{
*/
/** Singleton that manages the Animations.
It saves in a cache the animations. You should use this class if you want to save your animations in a cache.
Before v0.99.5, the recommend way was to save them on the CCSprite. Since v0.99.5, you should use this class instead.
@since v0.99.5
*/
class CC_DLL CCAnimationCache : public CCObject
{
public:
CCAnimationCache();
~CCAnimationCache();
/** Returns the shared instance of the Animation cache */
static CCAnimationCache* sharedAnimationCache(void); //返回动画存储池单例
/** Purges(清除) the cache. It releases all the CCAnimation objects and the shared instance.
*/
static void purgeSharedAnimationCache(void); //清空动画池
/** Adds a CCAnimation with a name.
*/
void addAnimation(CCAnimation *animation, const char * name); //往动画池里加动画 参数2是动画名字
/** Deletes a CCAnimation from the cache.
*/
void removeAnimationByName(const char* name);//删除动画池中动画 参数是动画名字
/** Returns a CCAnimation that was previously added.
If the name is not found it will return nil.
You should retain the returned copy if you are going to use it.
*/
CCAnimation* animationByName(const char* name);// 返回一个动画 参数是动画名字 使用前必须对返回的动画retain
/** Adds an animation from an NSDictionary
Make sure that the frames were previously loaded in the CCSpriteFrameCache.
@since v1.1
*/
void addAnimationsWithDictionary(CCDictionary* dictionary); //根据字典 创建动画并放入动画池 字典保存若干个 动画所需帧和动画名
{
CCDictionary* animations = (CCDictionary*)dictionary->objectForKey("animations");
if ( animations == NULL ) {
CCLOG("cocos2d: CCAnimationCache: No animations were found in provided dictionary.");
return;
}
unsigned int version = 1;
CCDictionary* properties = (CCDictionary*)dictionary->objectForKey("properties");
if( properties )
{
version = properties->valueForKey("format")->intValue();
CCArray* spritesheets = (CCArray*)properties->objectForKey("spritesheets");
CCObject* pObj = NULL;
CCARRAY_FOREACH(spritesheets, pObj)
{
CCString* name = (CCString*)(pObj);
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(name->getCString());
}
}
switch (version) {
case 1:
parseVersion1(animations);
break;
case 2:
parseVersion2(animations);
break;
default:
CCAssert(false, "Invalid animation format");
}
}
/////////////////////////
-(void)addAnimationsWithDiction
根据一个字典创建CCAnimation对象,并存入内存池。字典中保存的是每个动画的名字,动画中包含哪些帧,换帧间隔是多长。
-(void)addAnimationsWithFile:(NSString *)plist
根据一个plist文件创建CCAnimation对象,并存入内存池。先用文件中的信息生成一个字典,再调用addAnimationsWithDiction
/** Adds an animation from a plist file.
Make sure that the frames were previously loaded in the CCSpriteFrameCache.
@since v1.1
*/
void addAnimationsWithFile(const char* plist);
{
CCAssert( plist, "Invalid texture file name");
std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename(plist); //根据文件名得到文件路径
CCDictionary* dict = CCDictionary::createWithContentsOfFile(path.c_str()); //根据文件路径找到文件 打开创建字典
CCAssert( dict, "CCAnimationCache: File could not be found");
addAnimationsWithDictionary(dict);
}
bool init(void);
private:
void parseVersion1(CCDictionary* animations);
{
CCSpriteFrameCache *frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
CCDictElement* pElement = NULL;
CCDICT_FOREACH(animations, pElement)
{
CCDictionary* animationDict = (CCDictionary*)pElement->getObject();
CCArray* frameNames = (CCArray*)animationDict->objectForKey("frames");
float delay = animationDict->valueForKey("delay")->floatValue();
CCAnimation* animation = NULL;
if ( frameNames == NULL )
{
CCLOG("cocos2d: CCAnimationCache: Animation '%s' found in dictionary without any frames - cannot add to animation cache.", pElement->getStrKey());
continue;
}
CCArray* frames = CCArray::createWithCapacity(frameNames->count());
frames->retain();
CCObject* pObj = NULL;
CCARRAY_FOREACH(frameNames, pObj)
{
const char* frameName = ((CCString*)pObj)->getCString();
CCSpriteFrame* spriteFrame = frameCache->spriteFrameByName(frameName);
if ( ! spriteFrame ) {
CCLOG("cocos2d: CCAnimationCache: Animation '%s' refers to frame '%s' which is not currently in the CCSpriteFrameCache. This frame will not be added to the animation.", pElement->getStrKey(), frameName);
continue;
}
CCAnimationFrame* animFrame = new CCAnimationFrame();
animFrame->initWithSpriteFrame(spriteFrame, 1, NULL);
frames->addObject(animFrame);
animFrame->release();
}
if ( frames->count() == 0 ) {
CCLOG("cocos2d: CCAnimationCache: None of the frames for animation '%s' were found in the CCSpriteFrameCache. Animation is not being added to the Animation Cache.", pElement->getStrKey());
continue;
} else if ( frames->count() != frameNames->count() ) {
CCLOG("cocos2d: CCAnimationCache: An animation in your dictionary refers to a frame which is not in the CCSpriteFrameCache. Some or all of the frames for the animation '%s' may be missing.", pElement->getStrKey());
}
animation = CCAnimation::create(frames, delay, 1);
CCAnimationCache::sharedAnimationCache()->addAnimation(animation, pElement->getStrKey());
frames->release();
}
}
void parseVersion2(CCDictionary* animations);
private:
CCDictionary* m_pAnimations;
static CCAnimationCache* s_pSharedAnimationCache;
};
// end of sprite_nodes group
/// @}
NS_CC_END
#endif // __CC_ANIMATION_CACHE_H__