cocos2dx 源码学习2 CCAnimationCache

前端之家收集整理的这篇文章主要介绍了cocos2dx 源码学习2 CCAnimationCache前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我们经常会使用到CCSprite这个类,在cocos2dx源码中,用Visual Studio打开后他位于libcocos2d项目的sprite_node文件夹下:


CCAnimationCache类用来保存CCAnimation,这样后续就可以直接用于创建CCAnimate动画作用于CCSprite。下面看一下CCAnimationCache的源码:

一.公有成员函数


(1)从头文件中我们可以看到,最基本的类的构造函数和析构函数

(2)void addAnimation(CCAnimation *animation,const char * name);

添加一个CCAnimation对象后跟一个参数name,因为CCAnimationCache采用字典的形式来存储的,在cpp文件中我们可以看到这样的实现

void CCAnimationCache::addAnimation(CCAnimation *animation,const char * name)
{
m_pAnimations->setObject(animation,name);
}

m_pAnimations为CCAnimationCache类的私有变量,可以看出是一个字典类型

private:
CCDictionary* m_pAnimations;

(3)void removeAnimationByName(const char* name);

这个一看函数名就知道,是移除之前添加的对应name的CCAnimation 对象。cpp是这样实现的:

void CCAnimationCache::removeAnimationByName(const char* name)
{
if (! name)
{
return;
}


m_pAnimations->removeObjectForKey(name);
}

先判断name是不是为空,然后再移除。

(4)CCAnimation* animationByName(const char* name);

根据name在字典中查找CCAnimation,找到则返回此对象,用于后续CCSprite的动画。在cpp中是这样实现的

CCAnimation* CCAnimationCache::animationByName(const char* name)
{
return (CCAnimation*)m_pAnimations->objectForKey(name);
}

(5)void addAnimationsWithDictionary(CCDictionary* dictionary,const char* plist = NULL);

从一个字典中添加CCAnimation,就是说,我们先把CCAnimation存入字典中,在把这个字典作为参数。后跟一个plist,plist可传可不传,表示对应的plist文件,所以我们事先必须先用CCSpriteFrameCache把我们需要的资源文件加载。其中CCDictionary有一个函数createWithContentsOfFile,可以直接把plist文件中的动画直接保存在字典中。下面举一例子:

CCDictionary* pDict = CCDictionary::createWithContentsOfFile("animations.plist");

animCache->addAnimationsWithDictionary(pDict);


(6)void addAnimationsWithFile(const char* plist);

从一个plist中加载。我们看下源代码就知道他是如何实现的:

void CCAnimationCache::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,plist);
}

从中可以看出,其实是调用了前一个函数addAnimationsWithDictionary实现的,其中也用到了我们之前说的函数createWithContentsOfFile。

二.私有成员函数

除了上面的公有函数外,CCAnimationCache还有两个静态成员函数,其次CCAnimationCache被设计成了单例模式:


(1)static CCAnimationCache* sharedAnimationCache(void);

既是静态成员函数,也是单例实现函数

CCAnimationCache* CCAnimationCache::s_pSharedAnimationCache = NULL;


CCAnimationCache* CCAnimationCache::sharedAnimationCache(void)
{
if (! s_pSharedAnimationCache)
{
s_pSharedAnimationCache = new CCAnimationCache();
s_pSharedAnimationCache->init();
}


return s_pSharedAnimationCache;
}

在头文件中声明保护成员:

private:
CCDictionary* m_pAnimations;
static CCAnimationCache* s_pSharedAnimationCache;

如果我们想自己写的类也变成单例,我们也可以仿照这样来实现。

(2)static void purgeSharedAnimationCache(void);

函数用来释放单例对象,删除所有的保存的CCAnimation


下面例举官方实例TestCpp中的实现:

CCSpriteFrameCache *frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
frameCache->addSpriteFramesWithFile("grossini.plist");
frameCache->addSpriteFramesWithFile("grossini_gray.plist");
frameCache->addSpriteFramesWithFile("grossini_blue.plist");

// Purge prevIoUsly loaded animation
CCAnimationCache::purgeSharedAnimationCache();


CCAnimationCache *animCache = CCAnimationCache::sharedAnimationCache();
CCDictionary* pDict = CCDictionary::createWithContentsOfFile("animations.plist");

animCache->addAnimationsWithDictionary(pDict);//用字典创建
// Add an animation to the Cache
//animCache->addAnimationsWithFile("animations.plist"); //用plist文件创建


CCAnimation *normal = animCache->animationByName("dance_1");
normal->setRestoreOriginalFrame(true);
CCAnimation *dance_grey = animCache->animationByName("dance_2");
dance_grey->setRestoreOriginalFrame(true);
CCAnimation *dance_blue = animCache->animationByName("dance_3");
dance_blue->setRestoreOriginalFrame(true);

CCAnimate *animN = CCAnimate::create(normal);
CCAnimate *animG = CCAnimate::create(dance_grey);
CCAnimate *animB = CCAnimate::create(dance_blue);

CCSequence *seq = CCSequence::create(animN,animG,animB,NULL);

// create an sprite without texture
CCSprite *grossini = CCSprite::create();

CCSpriteFrame *frame = frameCache->spriteFrameByName("grossini_dance_01.png");
grossini->setDisplayFrame(frame);

CCSize winSize = CCDirector::sharedDirector()->getWinSize();

grossini->setPosition(ccp(winSize.width/2,winSize.height/2));

addChild(grossini);

// run the animation
grossini->runAction(seq);


pilst文件格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>animations</key>
<dict>
<key>dance_1</key>
<dict>
<key>delay</key>
<real>0.2</real>
<key>frames</key>
<array>
<string>grossini_dance_01.png</string>
<string>grossini_dance_02.png</string>
<string>grossini_dance_03.png</string>
<string>grossini_dance_04.png</string>
<string>grossini_dance_05.png</string>
<string>grossini_dance_06.png</string>
<string>grossini_dance_07.png</string>
<string>grossini_dance_08.png</string>
<string>grossini_dance_09.png</string>
<string>grossini_dance_10.png</string>
<string>grossini_dance_11.png</string>
<string>grossini_dance_12.png</string>
<string>grossini_dance_13.png</string>
<string>grossini_dance_14.png</string>
</array>
</dict>
<key>dance_2</key>
<dict>
<key>delay</key>
<real>0.2</real>
<key>frames</key>
<array>
<string>grossini_dance_gray_01.png</string>
<string>grossini_dance_gray_02.png</string>
<string>grossini_dance_gray_03.png</string>
<string>grossini_dance_gray_04.png</string>
<string>grossini_dance_gray_05.png</string>
<string>grossini_dance_gray_06.png</string>
<string>grossini_dance_gray_07.png</string>
<string>grossini_dance_gray_08.png</string>
<string>grossini_dance_gray_09.png</string>
<string>grossini_dance_gray_10.png</string>
<string>grossini_dance_gray_11.png</string>
<string>grossini_dance_gray_12.png</string>
<string>grossini_dance_gray_13.png</string>
<string>grossini_dance_gray_14.png</string>
</array>
</dict>
<key>dance_3</key>
<dict>
<key>delay</key>
<real>0.2</real>
<key>frames</key>
<array>
<string>grossini_blue_01.png</string>
<string>grossini_blue_02.png</string>
<string>grossini_blue_03.png</string>
<string>grossini_blue_04.png</string>
</array>
</dict>
</dict>
</dict>
</plist>


因为addAnimationsWithDictionary函数在进行解析时,是先查找animations关键字段。从源码可以看出:

void CCAnimationCache::addAnimationsWithDictionary(CCDictionary* dictionary,const char* plist) { 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); if (plist) { const char* path = CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile(name->getCString(),plist); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(path); } else { CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(name->getCString()); } } } switch (version) { case 1: parseVersion1(animations); break; case 2: parseVersion2(animations); break; default: CCAssert(false,"Invalid animation format"); } }

猜你在找的Cocos2d-x相关文章