Cocos2d-x 3.0 开发(七)在程序中处理cocoStudio导出动画
1、概述
使用cocoStudio可以方便的制作动画,接下来的工作就是在我们的程序中使用制作的动画。这篇中,我将使用程序将两个动画连接起来。有图有真相:
2、制作动画
承接上一篇,我们再制作一个动画。制作动画的方法与之前没有差别,不太熟悉的同学可以看:Cocos2d-x 3.0开发(六)使用cocoStudio创建一个骨骼动画。在“动作列表”中右击,“添加动画”然后编辑就成。
我们新制作的动画的结束点,要与上一篇中制作动画的开始点重合,这样在连接的时候,画面就不会跳动。
制作好后我们将动画导出。
3、制作UI
既然能够方便的制作UI,我就顺手做了一个控制动画播放的UI。制作方法之前也提到过。没有什么差别。使用UI编辑器制作UI,并将其导出。
4、关联到项目
运行脚本创建我们的项目,将导出的动画、UI放到Resource文件夹中。
然后重写init方法:
- bool HelloWorld::init()
- {
- //
- // 1. super init first
- if ( !Layer::init() )
- {
- return false;
- }
- Size visibleSize = Director::getInstance()->getVisibleSize();
- Point origin = Director::getInstance()->getVisibleOrigin();
- auto ui = dynamic_cast<Layout*>(CCUIHELPER->createWidgetFromJsonFile("ControlUI.ExportJson"));
- ui->getChildByTag(UI_BUTTON_PLAY1)->addTouchEventListener(this, toucheventselector(HelloWorld::touchCallBack));
- ui->getChildByTag(UI_BUTTON_PLAY2)->addTouchEventListener(this, toucheventselector(HelloWorld::touchCallBack));
- ui->getChildByTag(UI_BUTTON_CONN)->addTouchEventListener(this, toucheventselector(HelloWorld::touchCallBack));
- ui->getChildByTag(UI_BUTTON_DISCONN)->addTouchEventListener(this, toucheventselector(HelloWorld::touchCallBack));
- auto uiLayer = UILayer::create();
- uiLayer->addWidget(ui);
- this->addChild(uiLayer);
- return true;
- }
- void HelloWorld::touchCallBack(Object* obj,TouchEventType type)
- {
- //will play
- }
bool HelloWorld::init()
{//// 1. super init firstif ( !Layer::init() ){return false;}Size visibleSize = Director::getInstance()->getVisibleSize();Point origin = Director::getInstance()->getVisibleOrigin();auto ui = dynamic_cast<Layout*>(CCUIHELPER->createWidgetFromJsonFile("ControlUI.ExportJson"));ui->getChildByTag(UI_BUTTON_PLAY1)->addTouchEventListener(this, toucheventselector(HelloWorld::touchCallBack));ui->getChildByTag(UI_BUTTON_PLAY2)->addTouchEventListener(this, toucheventselector(HelloWorld::touchCallBack));ui->getChildByTag(UI_BUTTON_CONN)->addTouchEventListener(this, toucheventselector(HelloWorld::touchCallBack));ui->getChildByTag(UI_BUTTON_DISCONN)->addTouchEventListener(this, toucheventselector(HelloWorld::touchCallBack));auto uiLayer = UILayer::create();uiLayer->addWidget(ui);this->addChild(uiLayer);return true;
}void HelloWorld::touchCallBack(Object* obj,TouchEventType type)
{//will play
}
5、加载动画
动画的导出文件也是一个json。载入后被封装到一个Armature对象中。Armature是NodeRGBA的子类,所以它可以直接被addChild到父节点中。加载所用的是ArmatureManager中的方法。它是一个单例,管理整个场景中的Armature。我们在编辑器中编辑的动画是Animation,它被封装在Armature中了。因此这是一个三层的结构。ArmatureManager最大,然后是Armature,最后是Animation。我们播放动画用的都是Animation中的方法。
说完了原理,我们来看看代码。首先在init中添加加载Armature。
- ArmatureDataManager::getInstance()->addArmatureFileInfo("MyAnimation.ExportJson");
- Armature* armature = Armature::create("MyAnimation");
- armature->setTag(AM_MYANIMATION);
- armature->setPosition(Point(origin.x + visibleSize.width/2 ,
- origin.y + visibleSize.height/2));
- this->addChild(armature);
ArmatureDataManager::getInstance()->addArmatureFileInfo("MyAnimation.ExportJson");
Armature* armature = Armature::create("MyAnimation");
armature->setTag(AM_MYANIMATION); armature->setPosition(Point(origin.x + visibleSize.width/2 ,origin.y + visibleSize.height/2));
this->addChild(armature);
然后重写touchCallback方法控制播放动画。
- void HelloWorld::touchCallBack(Object* obj,TouchEventType type)
- {
- auto uiBt = dynamic_cast<UIButton*>(obj);
- if(!uiBt)
- {
- return;
- }
- int tag = uiBt->getTag();
- auto armature = (Armature*)getChildByTag(AM_MYANIMATION);
- switch (type)
- {
- case TouchEventType::TOUCH_EVENT_ENDED:
- if(tag == UI_BUTTON_PLAY1)
- {
- armature->getAnimation()->play("hit");
- }
- else if(tag ==UI_BUTTON_PLAY2)
- {
- armature->getAnimation()->play("fall");
- }
- else if(tag == UI_BUTTON_CONN)
- {
- //will conn
- }
- else if(tag == UI_BUTTON_DISCONN)
- {
- //will dis conn
- }
- break;
- default:
- break;
- }
- }
void HelloWorld::touchCallBack(Object* obj,TouchEventType type)
{auto uiBt = dynamic_cast<UIButton*>(obj);if(!uiBt){return;}int tag = uiBt->getTag();auto armature = (Armature*)getChildByTag(AM_MYANIMATION);switch (type){case TouchEventType::TOUCH_EVENT_ENDED:if(tag == UI_BUTTON_PLAY1){armature->getAnimation()->play("hit");}else if(tag ==UI_BUTTON_PLAY2){armature->getAnimation()->play("fall");}else if(tag == UI_BUTTON_CONN){//will conn}else if(tag == UI_BUTTON_DISCONN){//will dis conn}break;default:break;}
}
6、处理动画事件
在Animation中有动画事件的概念,每一个动画开始和结束都会事件。我们需要做的就是监听这个事件并为其写好响应函数。
所以接下来我们完善touchCallback函数,并添加一个监听函数。
- //......
- else if(tag == UI_BUTTON_CONN)
- {
- armature->getAnimation()->setMovementEventCallFunc(this,movementEvent_selector(HelloWorld::movementCallback));
- }
- else if(tag == UI_BUTTON_DISCONN)
- {
- armature->getAnimation()->setMovementEventCallFunc(this,nullptr);
- }
- //......
- void HelloWorld::movementCallback(Armature * armature, MovementEventType type, const char * name)
- {
- if (type == COMPLETE)
- {
- if (strcmp(name,"fall") == 0)
- {
- Armature* arm = (Armature*) getChildByTag(AM_MYANIMATION);
- arm->getAnimation()->play("hit");
- }
- }
- }
//......
else if(tag == UI_BUTTON_CONN)
{armature->getAnimation()->setMovementEventCallFunc(this,movementEvent_selector(HelloWorld::movementCallback));
}
else if(tag == UI_BUTTON_DISCONN)
{armature->getAnimation()->setMovementEventCallFunc(this,nullptr);
}
//......void HelloWorld::movementCallback(Armature * armature, MovementEventType type, const char * name)
{if (type == COMPLETE){if (strcmp(name,"fall") == 0){Armature* arm = (Armature*) getChildByTag(AM_MYANIMATION);arm->getAnimation()->play("hit");}}
}
编译运行,就可以看到动画连接起来了。
7、总结
通过ArmatureDataManager单例来加载动画,将其关联到程序中。动画事件的监听,对动画的行为进行处理。使用这些方法我们可以灵活的使用cocoStudio创建的动画了。
Demo下载:http://download.csdn.net/detail/fansongy/6439225
本篇博客出自阿修罗道,转载请注明出处,禁止用于商业用途:http://blog.csdn.net/fansongy/article/details/12955989
Cocos2d-x 3.0 开发(七)在程序中处理cocoStudio导出动画相关推荐
- CocoStudio基础教程(3)在程序中处理cocoStudio导出动画
1.概述 使用cocoStudio可以方便的制作动画,接下来的工作就是在我们的程序中使用制作的动画.这篇中,我将使用程序将两个动画连接起来 2.关联到项目 运行脚本创建我们的项目,将导出的动画.UI放 ...
- 开发微信小程序 中遇到的坑 及解决方法
开发微信小程序 中遇到的坑 及解决方法 参考文章: (1)开发微信小程序 中遇到的坑 及解决方法 (2)https://www.cnblogs.com/feiye512/p/6070052.html ...
- php与ipa接口登录验证失败,thinkPHP5.0开发微信小程序登录接口signature验证失败
我用TP5开发微信小程序登录接口的时候,在校验签名signature那遇到了个坑,一直校验失败,找了很久才发现 原因是:tp的input方法会自动转移html字符,比如'"'转成了 & ...
- 开发微信小程序中SSL协议的申请、证书绑定、TLS 版本处理等
在上篇随笔<基于微信小程序的系统开发准备工作>介绍了开发微信小程序的一些前期的架构设计.技术路线 .工具准备等方面内容,本篇随笔继续这个步骤,逐步介绍我们实际开发过程中对SSL协议的申请及 ...
- 让你提前认识软件开发(14):程序中的算法
第1部分 又一次认识C语言 程序中的算法 [文章摘要] 算法(Algorithm),是程序的灵魂.著名计算机科学家.图灵奖获得者沃思曾提出过一个公式:数据结构+算法=程序. 可见,算法 ...
- 手把手教你开发微信小程序中的插件
继上次 手把手教你实现微信小程序中的自定义组件 已经有一段时间了(不了解的小伙伴建议去看看,因为插件很多内容跟组件相似),今年3月13日,小程序新增了 小程序**「插件」 功能,以及开发者工具新增 「 ...
- 嵌入式开发:嵌入式应用程序中的信任链简介
在设计嵌入式系统时,考虑安全性已经成为许多行业的需要.将设备连接到互联网的驱动器允许对系统进行远程攻击.想要构建安全嵌入式系统的嵌入式开发人员必须确保他们的设备实现信任链.在这篇文章中,我们将探讨什么 ...
- ASP.NET Core 1.0开发Web API程序
.NET Core版本:1.0.0-rc2 Visual Studio版本:Microsoft Visual Studio Community 2015 Update 2 开发及运行平台:Window ...
- 记录uni.app开发微信小程序中地图的使用,以及项目中的解决办法
标题先讲一下需求:需要获取用户的地址信息以及经纬度,并在地图中展示时出来 uniapp官方也提供了api uni.getLocation(OBJECT) getLocation只能获取用户的经纬度,不 ...
- android程序怎么导出数据库,将android程序中的数据库导出到SD卡
private void copyDBToSDcrad() { String DATABASE_NAME = "数据库文件名"; String oldPath = "da ...
最新文章
- hihocoder offer收割编程练习赛11 B 物品价值
- arXiv论文如何一键链接解读视频,这个浏览器扩展帮你实现
- 人类首张脑电波连接全图问世
- 视图解析器中配置前缀和后缀---SpringMVC学习笔记(五)
- 22 款设计和可视化神经网络的工具
- CentOS 系列安装 Docker
- 零基础学习Python文本处理
- mysql执行脚本的方法
- 小程序的学习资料收集
- ogg怎么转mp3格式,ogg转mp3方法
- outlook qr码在哪里_QR和条码扫描仪app下载|QR和条码扫描仪安卓版下载_v10.00.3
- 随机生成验证码,并判断是否正确(String版)
- 商汤科技徐立:AI 将在10 年内创造一个印度和中国的总产值
- 当我们在谈论色温调节的时候,我们在谈论什么
- 计算机视觉 (CV) 相关的会议或期刊投稿时间
- 如何利用Excel快速批量创建文件夹
- linux at24c 前几个字节错误,I2C操作笔记——以 AT24C04为例
- 【国际电联无线电通信部门(ITU-R)】雨衰建议书、雨衰模型和降雨率数据的下载
- 西安交大城市学院计算机怎么,西安交大城市学院计算机系召开调研汇报会
- 怎么把视频做成gif表情包?