cocos2dx 源码学习2 CCAnimationCache

cocos2dx 源码学习2 CCAnimationCache,第1张

概述我们经常会使用到CCSprite这个类,在cocos2dx源码中,用Visual Studio打开后他位于libcocos2d项目的sprite_node文件夹下: CCAnimationCache类用来保存CCAnimation,这样后续就可以直接用于创建CCAnimate动画作用于CCSprite。下面看一下CCAnimationCache的源码: 一.公有成员函数 (1)从头文件中我们可以看

我们经常会使用到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"); } }

总结

以上是内存溢出为你收集整理的cocos2dx 源码学习2 CCAnimationCache全部内容,希望文章能够帮你解决cocos2dx 源码学习2 CCAnimationCache所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1010487.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存