版本:cocos2dx 2.2.6
IDE: VS2012
语言:C++98
美术资源一共有两段动画的序列帧,一个是手绘马行走图,一个是分子人行走图。
程序要实现的目的就是在同一个位置,点击按钮可以实现2段动画的切换。
因为动画最终是通过sprite的runAction执行的,所以我做了一个封装,返回一个带动画的精灵。
CCSprite* HelloWorld::createAnimateSprite( int start, int end, CCString* startFrame, CCString* formatStr)
{
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCSprite* pSprite = CCSprite::createWithSpriteFrameName(startFrame->getCString());
pSprite->setPosition(ccp(origin.x + visibleSize.width / , origin.y + visibleSize.height / ));
CCArray* pArray = CCArray::create(); char name[];
for (int i = start;i <= end; i++)
{
sprintf(name, formatStr->getCString(), i);
pArray->addObject(CCSpriteFrameCache::sharedSpriteFrameCache() ->spriteFrameByName(name));
} CCAnimation* pAnimation = CCAnimation::createWithSpriteFrames(pArray,0.1f);
CCAnimate* pAnimate = CCAnimate::create(pAnimation);
pSprite->runAction(CCRepeatForever::create(pAnimate));
return pSprite;
}
函数签名中的start和end表示图片名称后缀的起始数字和结束数字,startFrame是起始动画帧的名称,format是通用格式,配合数字组成完整的动画帧名称
CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("walk.plist");
this->addChild(createAnimateSprite(, , CCString::create("zzlx1.JPG"), CCString::create("zzlx%d.JPG")), , );
this->getChildByTag()->setVisible(true);
this->addChild(createAnimateSprite(, , CCString::create("Horse1.jpg"), CCString::create("Horse%d.jpg")), , );
this->getChildByTag()->setVisible(false);
将2个带动画的精灵加入层中,然后在鼠标点击的回调中进行动画的切换,切换采用设置sprite的visible属性的方式
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCNode* child1 = this->getChildByTag();
CCNode* child2 = this->getChildByTag();
bool flag = child1->isVisible();
child1->setVisible(!flag);
child2->setVisible(flag);
}
上真相
还有另外一种方法来实现动画的切换,使用AnimationCache和ActionManager,这样是从本质上移除A动画,加入B动画
void HelloWorld::createAnimation( int start, int end, CCString* formatStr, CCString* animationName )
{
CCArray* pArray = CCArray::create(); char name[];
for (int i = start;i <= end; i++)
{
sprintf(name, formatStr->getCString(), i);
pArray->addObject(CCSpriteFrameCache::sharedSpriteFrameCache() ->spriteFrameByName(name));
} CCAnimation* pAnimation = CCAnimation::createWithSpriteFrames(pArray,0.1f);
CCAnimationCache::sharedAnimationCache()->addAnimation(pAnimation,animationName->getCString());
}
这里是将一段动画加入AnimationCache,并且设置对应的key,然后通过动画缓存创建一个动画精灵
CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("walk.plist");
createAnimation(, , CCString::create("zzlx%d.JPG"), CCString::create("man"));
createAnimation(, , CCString::create("Horse%d.jpg"), CCString::create("horse")); CCSprite* pSprite = CCSprite::createWithSpriteFrameName("zzlx1.JPG");
pSprite->setPosition(ccp(origin.x + visibleSize.width / , origin.y + visibleSize.height / ));
pSprite->runAction(CCRepeatForever::create(CCAnimate::create(CCAnimationCache::sharedAnimationCache()->animationByName("man"))));
this->addChild(pSprite, , );
上面代码把2段动画man和horse加入了动画缓存,在通过key"man"创建了一个动画精灵
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
static std::string str = "man";
CCNode* node = this->getChildByTag();
CCActionManager* actionManager = CCDirector::sharedDirector()->getActionManager();
actionManager->removeAllActionsFromTarget(this->getChildByTag());
if (str == "man")
{
str = "horse";
}
else
{
str = "man";
}
node->runAction(CCRepeatForever::create(CCAnimate::create(CCAnimationCache::sharedAnimationCache()->animationByName(str.c_str()))));
}
CCActionManager管理所有对象的action,removeAllActionsFromTarget可以移除指定对象的所有动画,先移除再添加另外一段动画则完成了切换功能。
CCActionManager还有几种移除的api,比如可以移除特定对象特定tag的action,可以灵活的使用。
上图
如果对一个动画需要暂停和继续播放功能需要使用ccnode的pauseSchedulerAndActions和resumeSchedulerAndActions方法
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
static bool flag = false;
if (!flag)
{
this->getChildByTag()->pauseSchedulerAndActions();
flag = true;
}
else
{
this->getChildByTag()->resumeSchedulerAndActions();
flag = false;
}
}