关于如何使用CocoStudio导出动画文件这里就不介绍了,网上教程很多。这里主要介绍动画播放的控制

1.添加获取第几个动画函数,看了下CCArmatureAnimation文件,系统没有获取当前第几个动画的接口,这里添加下,很简单,红色部分为添加

void CCArmatureAnimation::playByIndex(int animationIndex, int durationTo, int durationTween,  int loop, int tweenEasing)
{
    std::vector<std::string> &movName = m_pAnimationData->movementNames;
    CC_ASSERT((animationIndex > -1) && ((unsigned int)animationIndex < movName.size()));

animIndex = animationIndex;
    std::string animationName = movName.at(animationIndex);
    play(animationName.c_str(), durationTo, durationTween, loop, tweenEasing);
}

/*获取当前播放的是第几个动画*/
int CCArmatureAnimation::getAnimIndex()
{
std::vector<std::string> &movName = m_pAnimationData->movementNames;
CC_ASSERT((animIndex > -1) && ((unsigned int)animIndex < movName.size()));
return animIndex;
}

在.h添加成员变量:int animIndex;

2.添加获取当前播放动画的总帧数函数

int CCArmatureAnimation::getFrameCount()
{
return m_iDurationTween-1;
}

3.添加ui

//界面编辑器
/  
UILayer* uiLayer = UILayer::create();  
auto myLayout = dynamic_cast<Layout*>(CCUIHELPER->createWidgetFromJsonFile("CocoStudioUI/DemoShop.ExportJson"));  
uiLayer->addWidget(myLayout);  
this->addChild(uiLayer);
UILabelBMFont*button_text = dynamic_cast<UILabelBMFont*>(myLayout->getChildByName("back_LabelBMFont"));
button_text->setTouchEnable(false);//屏蔽按钮上文字的触摸
UIButton*button = dynamic_cast<UIButton*>(myLayout->getChildByName("back_Button"));
button->addReleaseEvent(this,coco_releaseselector(HelloWorld::UI_BackButton));//添加按键放开的回掉函数

4.添加场景编辑器:

// 加载场景资源

CCNode *pNode = CCSSceneReader::sharedSceneReader()->createNodeWithSceneFile("Scene/FightScene.json");
if (pNode)

{

this->addChild(pNode);  
}
// pNode 为 之前所获取的场景资源根节点,通过此节点获取到动画对象,获取方式根据在场景编辑其中设置的层次关系而定
CCComRender *pLoadRenderAni = (CCComRender*)(pNode->getChildByTag(10005)->getComponent("CCArmature"));
CCArmature* armLoad = (CCArmature*)(pLoadRenderAni->getNode());

// 播放动画
armLoad->getAnimation()->playByIndex(1);

//获取头像的精灵,缩放大小为2
CCComRender *pLoadRenderUI = (CCComRender*)(pNode->getChildByTag(10007)->getChildByTag(10011)->getComponent("CCSprite"));
CCSprite* headLoad = (CCSprite*)(pLoadRenderUI->getNode());
headLoad->setScale(2.0f);

我们要实现的时点击按钮切换动画,动画播放到最后一帧便停止动画,实现代码如下(下面没实现场景编辑器代码):

HelloWorld .h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
//引入扩展类
#include "cocos-ext.h"
//添加命名空间
using namespace cocos2d::extension;

class HelloWorld : public cocos2d::CCLayer
{
public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();

// there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::CCScene* scene();

void update(float dt); 
    // a selector callback
    void menuCloseCallback(CCObject* pSender);
    //UI button selector callback
    void UI_BackButton(CCObject *pSender);
    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);
private:
CCArmature* armature;
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorld.cpp如下:

#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

// add layer as a child to scene
    scene->addChild(layer);

// return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
bool bRet = false;  
do   
{  
//  
// super init first  
//

CC_BREAK_IF(! CCLayer::init());

CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();  
CCSize winSize = CCDirector::sharedDirector()->getWinSize();  
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

//界面编辑器
/  
UILayer* uiLayer = UILayer::create();  
auto myLayout = dynamic_cast<Layout*>(CCUIHELPER->createWidgetFromJsonFile("CocoStudioUI/DemoShop.ExportJson"));  
uiLayer->addWidget(myLayout);  
this->addChild(uiLayer);
UILabelBMFont*button_text = dynamic_cast<UILabelBMFont*>(myLayout->getChildByName("back_LabelBMFont"));
button_text->setTouchEnable(false);//屏蔽按钮上文字的触摸
UIButton*button = dynamic_cast<UIButton*>(myLayout->getChildByName("back_Button"));
button->addReleaseEvent(this,coco_releaseselector(HelloWorld::UI_BackButton));//添加按键放开的回掉函数

//动画
CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("CocoStudioAnim/CaAnima.ExportJson");    
armature = CCArmature::create("CaAnima");    
armature->setTag(1);        
CCPoint temp =armature->convertToNodeSpaceAR(ccp(100,100));  
armature->setScale(0.3f);  
armature->getAnimation()->playByIndex(1); //设置为第2个动作 
armature->getAnimation()->setAnimationInternal(0.1);//设置动画帧时间间隔

this->addChild(armature);    
armature->setPosition(300,200);

// add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));

// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);

bRet = true;

} while (0);  
this->scheduleUpdate();
return bRet; 
}

void HelloWorld::update(float dt)  
{  
//如果动画是最后一桢,则停止动画播放
if (armature->getAnimation()->getIsPlaying()&&armature->getAnimation()->getCurrentFrameIndex()== armature->getAnimation()->getFrameCount())
{
CCLOG("update frame count is%d totleCount is%d",armature->getAnimation()->getCurrentFrameIndex(),armature->getAnimation()->getFrameCount()); 
armature->getAnimation()->pause();
}
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if 0
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
#else
if (armature->getAnimation()->getAnimIndex() == 0)
{
armature->getAnimation()->playByIndex(1); //如果是第一个动作设置为第2个动作
}
else if (armature->getAnimation()->getAnimIndex() == 1)
{
armature->getAnimation()->playByIndex(0); //如果是第二个动作设置为第1个动作
}

#endif

}
void HelloWorld::UI_BackButton(CCObject *pSender)
{
CCLOG("UI_BackButtonReleased");
}

cocos2d-x 2.2 CocoStudio动画和界面编辑器按钮控制以及场景编辑器使用相关推荐

  1. Unity3d制作动画顺便加载按钮控制

    先上图: 我主要做了几件事:1.导入kl的unity package并拼接动画,2.添加两个UI中的按钮,Canvas是加载按钮时自动生成的,3.同时用代码实现了另外4个按钮的生成,效果图如下: 不管 ...

  2. 实习小白::(转) Cocostudio动画编辑器的使用

    今 天研究了一下cocostudio,包括动画编辑器和UI编辑器,其他的俩个感觉资料很少,就没有看了,等以后再说吧.先来说一下动画编辑器的使用吧.我 将把我用到的资源和这个cocostudio在文章的 ...

  3. 解决cocos2dx 3.x 导入cocostudio的ui界面出现错位问题

    笔者今天发现导入cocostudio的ui界面时,会有部分控件出现错位的现象,后来我看了一下源码,发现是部分控件是没有继承 Layout类,导致不能设置控件位置造成,原因可以看看cocos2dx 源码 ...

  4. Cocos2D研究院之精灵与动画

    转载自雨松MOMO程序研究院本文链接地址:Cocos2D研究院之精灵与动画(六) 通过对导演.场景.层和节点的剖析,现在我们已经可以写出一个完整的游戏体系了,在实际应用中,场景一般都是作为游戏的关卡, ...

  5. Android跳转动画时长,Android_Activit跳转动画之界面上某个位置并裂开上下拉伸动画跳转,需求:Activity(fragment)跳转的时候 - phpStudy...

    Activit跳转动画之界面上某个位置并裂开上下拉伸动画跳转 需求:Activity(fragment)跳转的时候当前界面裂开,上下各自拉出手机屏幕,之后跳转到相对应的Activity.整体效果图如下 ...

  6. 如何使用CocoStudio场景编辑器制作魔卡幻想

    1 CocoStudio 场景编辑器 使用 CocoStudio 场景编辑器来创建游戏场景,其中包含游戏 UI 与动画,来定制一个游戏主界面. 2 使用 CocoStudio 完成<魔卡幻想&g ...

  7. 【cocos2d-x】如何使用CocoStudio场景编辑器制作魔卡幻想

    1 CocoStudio 场景编辑器 使用 CocoStudio 场景编辑器来创建游戏场景,其中包含游戏 UI 与动画,来定制一个游戏主界面. 2 使用 CocoStudio 完成<魔卡幻想&g ...

  8. 美妙的 CSS3 动画!一组梦幻般的按钮效果

    今天给大家带来的是五款梦幻般的动画按钮效果.下面是在线演示,把鼠标放在按钮上试试,有惊喜哦!CSS3 引入了众多供功能强大的新特性,让设计和开发人员能够轻松的创作出各种精美的界面效果. 温馨提示:为保 ...

  9. Adobe Illustrator CS6 界面文字按钮太小,高分屏win10PS/AI等软件界面字太小解决方法

    Adobe Illustrator CS6 界面文字按钮太小,高分屏win10PS/AI等软件界面字太小解决方法 参考文章: (1)Adobe Illustrator CS6 界面文字按钮太小,高分屏 ...

  10. flash一个按钮控制动画_flutter闪屏过渡动画,闪光占位动画

    在程序设计的理念中,讲究一切都来源于物理世界,在现实世界中,人们在每接触到一个新的事物或者说在手指触碰到一个事物时,总是心里默许期望有一个反馈效果,这就是来源于心底深处常常被人忽略的一个潜在期望. 在 ...

最新文章

  1. MySQL:数据操作
  2. 【软件】chrome设置默认字体
  3. python序列类型-python序列类型有哪些
  4. 1.2 日期/时间的程序
  5. STM32F105的时钟配置
  6. 为安装创建软链接,迁移文件夹
  7. Java内存模型解析
  8. javascript函数式_JavaScript中的函数式编程原理
  9. Equation漏洞混淆利用分析总结(下)
  10. 7-130 古风排版 (20 分)
  11. 绿茶软件测试自学,7号心理测试小程序
  12. CentOS7上使用bind9搭建DNS主从服务器
  13. 安卓智能手机运行iFIX组态软件
  14. centos下修改mysql默认端口_centos7修改mysql默认端口
  15. FIT2CLOUD飞致云旗下开源项目DataEase成功进入GitHub趋势榜主榜
  16. 【转】NAT穿透技术
  17. 浙江2段线能上什么计算机学校,二段线考生看过来!这些浙江省内热门高校还有热门专业可捡漏...
  18. 小师妹教我如何轻松拿下BAT、网易、蘑菇街的offer
  19. 深度学习CPU版本环境搭建(从anaconda->pycharm->tensorflow)
  20. 三种Lp范数距离定义

热门文章

  1. 精锐万能票据打印专家
  2. Python3 色情图片识别
  3. GB28181协议之语音对讲
  4. 工程流体力学笔记暂记35 (平板层流边界层和平板混合边界层)
  5. 你的公司建立了企业文化了么?没有就看看这个
  6. 用MATLAB 读写各种文件 ∈ Matlab 使用笔记
  7. 九节点潮流计算matlab,(完整版)电力系统分析大作业matlab三机九节点潮流计算报告...
  8. 【MM32F5270开发板试用】基于MindSDK对接雨滴传感器
  9. Chrome 中迅雷的插件(正确的下载地址)下载以及安装(一)
  10. system什么意思c语言,system是什么意思?