zoukankan      html  css  js  c++  java
  • 实习小白::(转) Cocos2d-x 3.0 开发(七)在程序中处理cocoStudio导出动画

    1、概述

        使用cocoStudio可以方便的制作动画,接下来的工作就是在我们的程序中使用制作的动画。这篇中,我将使用程序将两个动画连接起来。有图有真相:

    2、制作动画

        承接上一篇,我们再制作一个动画。制作动画的方法与之前没有差别,不太熟悉的同学可以看:Cocos2d-x 3.0开发(六)使用cocoStudio创建一个骨骼动画。在“动作列表”中右击,“添加动画”然后编辑就成。

        我们新制作的动画的结束点,要与上一篇中制作动画的开始点重合,这样在连接的时候,画面就不会跳动。

     

        制作好后我们将动画导出。

    3、制作UI

        既然能够方便的制作UI,我就顺手做了一个控制动画播放的UI。制作方法之前也提到过。没有什么差别。使用UI编辑器制作UI,并将其导出。

    4、关联到项目

        运行脚本创建我们的项目,将导出的动画、UI放到Resource文件夹中。

        然后重写init方法:

    1. bool HelloWorld::init()  
    2. {  
    3.     //////////////////////////////  
    4.     // 1. super init first  
    5.     if ( !Layer::init() )  
    6.     {  
    7.         return false;  
    8.     }  
    9.       
    10.     Size visibleSize = Director::getInstance()->getVisibleSize();  
    11.     Point origin = Director::getInstance()->getVisibleOrigin();  
    12.   
    13.     auto ui = dynamic_cast<Layout*>(CCUIHELPER->createWidgetFromJsonFile("ControlUI.ExportJson"));  
    14.     ui->getChildByTag(UI_BUTTON_PLAY1)->addTouchEventListener(this, toucheventselector(HelloWorld::touchCallBack));  
    15.     ui->getChildByTag(UI_BUTTON_PLAY2)->addTouchEventListener(this, toucheventselector(HelloWorld::touchCallBack));  
    16.     ui->getChildByTag(UI_BUTTON_CONN)->addTouchEventListener(this, toucheventselector(HelloWorld::touchCallBack));  
    17.     ui->getChildByTag(UI_BUTTON_DISCONN)->addTouchEventListener(this, toucheventselector(HelloWorld::touchCallBack));  
    18.   
    19.     auto uiLayer = UILayer::create();  
    20.     uiLayer->addWidget(ui);  
    21.     this->addChild(uiLayer);  
    22.   
    23.     return true;  
    24. }  
    25.   
    26. void HelloWorld::touchCallBack(Object* obj,TouchEventType type)  
    27. {  
    28.     //will play   
    29. }  

    5、加载动画

        动画的导出文件也是一个json。载入后被封装到一个Armature对象中。Armature是NodeRGBA的子类,所以它可以直接被 addChild到父节点中。加载所用的是ArmatureManager中的方法。它是一个单例,管理整个场景中的Armature。我们在编辑器中编 辑的动画是Animation,它被封装在Armature中了。因此这是一个三层的结构。ArmatureManager最大,然后是 Armature,最后是Animation。我们播放动画用的都是Animation中的方法。

        说完了原理,我们来看看代码。首先在init中添加加载Armature。

    1. ArmatureDataManager::getInstance()->addArmatureFileInfo("MyAnimation.ExportJson");  
    2. Armature* armature = Armature::create("MyAnimation");  
    3. armature->setTag(AM_MYANIMATION);      
    4.   
    5. armature->setPosition(Point(origin.x + visibleSize.width/2 ,  
    6.                                 origin.y + visibleSize.height/2));  
    7. this->addChild(armature);  

        然后重写touchCallback方法控制播放动画。

    1. void HelloWorld::touchCallBack(Object* obj,TouchEventType type)  
    2. {  
    3.     auto uiBt = dynamic_cast<UIButton*>(obj);  
    4.     if(!uiBt)  
    5.     {  
    6.         return;  
    7.     }  
    8.     int tag = uiBt->getTag();  
    9.     auto armature = (Armature*)getChildByTag(AM_MYANIMATION);  
    10.     switch (type)  
    11.     {  
    12.     case TouchEventType::TOUCH_EVENT_ENDED:  
    13.             if(tag == UI_BUTTON_PLAY1)  
    14.             {  
    15.                 armature->getAnimation()->play("hit");  
    16.             }  
    17.             else if(tag ==UI_BUTTON_PLAY2)  
    18.             {  
    19.                 armature->getAnimation()->play("fall");  
    20.             }  
    21.             else if(tag == UI_BUTTON_CONN)  
    22.             {  
    23.                 //will conn  
    24.             }  
    25.             else if(tag == UI_BUTTON_DISCONN)  
    26.             {  
    27.                 //will dis conn  
    28.             }  
    29.             break;  
    30.     default:  
    31.         break;  
    32.     }  
    33. }  

    6、处理动画事件

        在Animation中有动画事件的概念,每一个动画开始和结束都会事件。我们需要做的就是监听这个事件并为其写好响应函数。

        所以接下来我们完善touchCallback函数,并添加一个监听函数。

     

    1. //......  
    2. else if(tag == UI_BUTTON_CONN)  
    3. {  
    4.     armature->getAnimation()->setMovementEventCallFunc(this,movementEvent_selector(HelloWorld::movementCallback));  
    5. }  
    6. else if(tag == UI_BUTTON_DISCONN)  
    7. {  
    8.     armature->getAnimation()->setMovementEventCallFunc(this,nullptr);  
    9. }  
    10. //......  
    11.   
    12. void HelloWorld::movementCallback(Armature * armature, MovementEventType type, const char * name)  
    13. {  
    14.     if (type == COMPLETE)  
    15.     {  
    16.         if (strcmp(name,"fall") == 0)  
    17.         {  
    18.             Armature* arm = (Armature*) getChildByTag(AM_MYANIMATION);  
    19.             arm->getAnimation()->play("hit");  
    20.         }  
    21.     }  
    22. }  



        编译运行,就可以看到动画连接起来了。

     

    7、总结

        通过ArmatureDataManager单例来加载动画,将其关联到程序中。动画事件的监听,对动画的行为进行处理。使用这些方法我们可以灵活的使用cocoStudio创建的动画了。

     

        Demo下载:http://download.csdn.net/detail/fansongy/6439225

     

     

        本篇博客出自阿修罗道,转载请注明出处,禁止用于商业用途:http://blog.csdn.net/fansongy/article/details/12955989 


  • 相关阅读:
    eclipse或adt-bundle创建的android项目没有自动生成MainActivity.java和activity_main.xml等文件解决办法
    递归遍历删除注册表项和键值
    注册表:无法打开 XXX 由于某个错误无法打开该密钥。详细信息:拒绝访问
    connot find one or more components. please reinstall the application
    Couldn't load libPassword from loader:NDK开发中C文件编译成cpu对应的so类库时,找不到类库报错的原因之一
    通用组合算法
    Chance – 功能强大的 JavaScript 随机数生成类库
    shell中各种括号的作用()、(())、[]、[[]]、{}
    iftop监控网络流量命令
    Linux—shell中$(( ))、$( )、``与${ }的区别
  • 原文地址:https://www.cnblogs.com/dudu580231/p/4983691.html
Copyright © 2011-2022 走看看