cocos2d-x骨骼動畫使用實例

#include "HelloWorldScene.h"

USING_NS_CC;

#define size Director::getInstance()->getVisibleSize()
Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    auto layer = HelloWorld::create();
    scene->addChild(layer);
    return scene;
}

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }

    //加載骨骼動畫的文件
    ArmatureDataManager::getInstance()->addArmatureFileInfo("Cowboy0.png", "Cowboy0.plist", "Cowboy.ExportJson");
    
    //使用骨骼動畫

        //創建動畫對象
    auto armature = Armature::create("Cowboy");
    armature->setPosition(Vec2(size.width/2, size.height/2));
    
    armature->setScale(0.1);
    this->addChild(armature);
    
        //通過下標播放動畫
    //    armature->getAnimation()->playWithIndex(5);
        //通過動作名稱播放動畫
    armature->getAnimation()->play("Fire");
        //設置動作執行速度(大於1 加速, 小於1 減速)
    armature->getAnimation()->setSpeedScale(0.4);
    //設置回調函數
    armature->getAnimation()->setMovementEventCallFunc(CC_CALLBACK_3(HelloWorld::animationEvent, this));
    
    return true;
}

void HelloWorld::animationEvent(Armature *armature,MovementEventType movementType,const std::string&movementID){
    //判斷動作是否完成
    if (movementType == LOOP_COMPLETE)
    {
        if (movementID == "Fire" )
        {
            armature->stopAllActions();
            auto actionToRight = MoveTo::create(2, Vec2(50, size.height/2));
            armature->runAction(Sequence::create(actionToRight,CallFuncN::create(CC_CALLBACK_1(HelloWorld::callback1, this)) , NULL));
            armature->getAnimation()->play("Walk");
        }
        else if (movementID == "FireMax")
        {
            armature->stopAllActions();
            auto actionToRight = MoveTo::create(2, Vec2(430, size.height/2));
            armature->runAction(Sequence::create(actionToRight,CallFuncN::create(CC_CALLBACK_1(HelloWorld::callback2, this)) , NULL));
            armature->getAnimation()->play("Walk");
        }
    }
    
    
}
void HelloWorld::callback1(Node*sender){
    auto armature =(Armature*)sender;
    armature->runAction(ScaleTo::create(0.3f, -0.1f, 0.1f));  //第二個參數實現轉身
    armature->getAnimation()->play("FireMax",10);
}
void HelloWorld::callback2(Node*sender){
    auto armature =(Armature*)sender;
    armature->runAction(ScaleTo::create(0.3f, 0.1f, 0.1f));
    armature->getAnimation()->play("Fire",10);
    
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章