zoukankan      html  css  js  c++  java
  • CocoStudio基础教程(3)在程序中处理cocoStudio导出动画

    1、概述

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

    2、关联到项目

        运行脚本创建我们的项目,将导出的动画、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   
    }  

    3、加载动画

        动画的导出文件也是一个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); 

    然后重写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)  
                {  
                    armature->getAnimation()->setMovementEventCallFunc(this,movementEvent_selector(HelloWorld::movementCallback)); 
                }  
                else if(tag == UI_BUTTON_DISCONN)  
                {  
                    armature->getAnimation()->setMovementEventCallFunc(this,nullptr);  
                }  
                break;  
        default:  
            break;  
        }  
    }  

    4、处理动画事件

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

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

    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");  
            }  
        }  
    } 

    5、总结

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

  • 相关阅读:
    树上差分
    循环数组最大子段和(带限制的最大子段和,单调队列优化)
    sprintf函数的用法详解
    VBS基础篇
    VBS基础篇
    VBS基础篇
    VBS基础篇
    VBS基础篇
    Android随笔
    Android随笔
  • 原文地址:https://www.cnblogs.com/damowang/p/4835841.html
Copyright © 2011-2022 走看看