cocos2dx切换播放的动画
版本: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;
}
}

cocos2dx切换播放的动画的更多相关文章
- [Cocos2d-x v3.x]序列帧动画
简单介绍 Cocos2d-x中.动画的详细内容是依靠精灵显示出来的,为了显示动态图片,我们须要不停切换精灵显示的内容.通过把静态的精灵变为动画播放器从而实现动画效果. 动画由帧组成,每一帧都是一个 ...
- Cocos2d-x Lua中帧动画
帧动画就是按一定时间间隔.一定的顺序.一帧一帧地显示帧图片.我们的美工要为精灵的运动绘制每一帧图片,因此帧动画会由很多帧组成,按照一定的顺序切换这些图片就可以了. 在Cocos2d-x Lua中播放帧 ...
- jQuery演示10种不同的切换图片列表动画效果以及tab动画演示 2
很常用的一款特效纯CSS完成tab实现5种不同切换对应内容效果 实例预览 下载地址 实例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...
- use SWF / Flash in cocos2d-x; cocos2d(cocos2d-x) 直接播放flash / SWF文件
前段时间移植一个页游到手游,原先页游的项目里面有1000+的Flash人物,宠物动画,特效. 这要是全部重新做一遍,还不累死人?所以就想干脆直接在Cocos2d(x)里面播放SWF文件.(包括场景,过 ...
- [置顶] 使用红孩儿工具箱完成基于Cocos2d-x的简单游戏动画界面
[Cocos2d-x相关教程来源于红孩儿的游戏编程之路CSDN博客地址:http://blog.csdn.net/honghaier 红孩儿Cocos2d-X学习园地QQ3群:205100149,47 ...
- 关于Cocos Creator用js脚本代码播放骨骼动画的步骤和注意事项
步骤: 1.用cc.find()方法找到相应的骨骼动画节点,并把这个对象赋值给一个var出来的新对象. 具体代码:var spineboy_anim = cc.find("UI_Root/a ...
- Android播放gif动画,增加屏幕掉金币效果
前言:播放gif的版本有很多,我这边使用Android自带的Movie类播放gif动画,也是在别人的基础上进行修改.有同样需求的朋友可以参考我的demo. 1.效果图如下: 2.部分主要代码 Main ...
- jQuery演示10种不同的切换图片列表动画效果
经常用到的图片插件演示jQuery十种不同的切换图片列表动画效果 在线演示 下载地址 实例代码 <!DOCTYPE html> <html lang="en" c ...
- HTML5骨骼动画Demo | 使用min2d、createjs、pixi播放spine动画
Spine做骨骼动画是比较流行的,使用起来可能相对复杂,但功能毕竟强大,所以市场占有率较大. 在unity.cocos2d.starling中使用spine已经很成熟了,而HTML5这一块可能刚刚起步 ...
随机推荐
- SLIP 协议
SLIP 协议 SLIP 英文原义:Serial Line Internet Protocol 中文释义:串行线路网际协议 注解:该协议是Windows远程访问的一种旧工业标准,主要在Unix远程访问 ...
- mysql的master和slave同步方案
同步原理 master将改变记录到二进制日志(binary log)中 slave将master的binary log events拷贝到它的中继日志(relay log) slave重做中继日志中的 ...
- matlab图像处理注意溢出!先要im2double!
imagedata_comb=imagedata_ebic*addnumber_ebic+imagedata_sem*addnumber_sem; %注意溢出啊!!!uint8最大值是255,也就是说 ...
- threejs 画二维圆(圆弧)
画圆: var radius = 40, segments = 64, material = new THREE.LineBasicMaterial({ color: 0x0000ff }), geo ...
- 监听器(Listener)学习(一)
一.监听器介绍 1.1.监听器的概念 监听器是一个专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动.监听器其实就是一个实现特定接口的普通 ...
- Python学习-类
类是对象的模板或蓝图,类是对象的抽象化,对象是类的实例化 在python中,一个对象的特征也称为属性(attribute),它所具有的的行为也称为方法(method) 对象 = 属性(特征)+方法(行 ...
- ubuntu qq安装
安装最新版qq2016 qq2012下载链接: https://pan.baidu.com/s/1miFVc04 密码: 3g9w 先解压到自己所在的目录,解压命令自己百度在这个目录下,有三个解压包, ...
- Django基于Form之登录和注册
1.创建Forms文件,内容略多,大家将就着看,不懂请留言 #!/usr/bin/env python # -*- coding: utf8 -*- #__Author: "Skiler H ...
- Django的url别名功能的使用
- URL: from django.urls import reverse url(r'^all/(?<article_type_id>\d+).html$', home.index, ...
- Sprint第一个冲刺(第十天)
一.Sprint介绍 更新工程部署文件:实现了云端登录:设计经营情况以及数据分析界面. 实验截图: 任务进度: 二.Sprint周期 看板: 燃尽图: