cocos2d-x骨骼动画使用

前端之家收集整理的这篇文章主要介绍了cocos2d-x骨骼动画使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、加入Dragonbones支持


二、加载文件

DBCCFactory::getInstance()->loadDragonBonesData("fileName/skeleton.xml","fileName");
DBCCFactory::getInstance()->loadTextureAtlas("fileName/texture.xml","fileName");

三、调用动画

DBCCArmatureNode * _armatureNode = nullptr;
this->setOpacity(0);//原来的设置不可见
if(this->getChildByName(ARMATURENODE)){
dynamic_cast<DBCCArmatureNode*>(this->getChildByName(ARMATURENODE))->getAnimation()->gotoAndPlay("动作名称");
}else {
_armatureNode = DBCCFactory::getInstance()->buildArmatureNode("fla_tianshu");
_armatureNode->setName(ARMATURENODE);
_armatureNode->setScale(0.5);
_armatureNode->setPosition(_VEC2(33,42));//设置位置
this->addChild(_armatureNode);
_armatureNode->getAnimation()->gotoAndPlay("z_stand");
}
if(_armatureNode){
_armatureNode->getCCEventDispatcher()->addCustomEventListener(EventData::COMPLETE,CC_CALLBACK_1(eventHandler,this));//非循环动作结束监听
_armatureNode->getCCEventDispatcher()->addCustomEventListener(EventData::LOOP_COMPLETE,this));//循环动作结束监听
}

四、监听动作结束

void eventHandler(EventCustom *event) { char *stands[] = {"z_stand","z_stand_b"}; EventData *eventData = (EventData*)(event->getUserData()); if (eventData){ DBCCArmatureNode * _armatureNode = dynamic_cast<DBCCArmatureNode*>(this->getChildByName(ARMATURENODE)); if(!_armatureNode){ CCASSERT(_armatureNode != nullptr,"_armatureNode is null"); return ; } switch(eventData->getType()){ case EventData::EventType::COMPLETE:{ if(this->getEnemyType()==EnemyType8){ if(eventData->animationState->name.compare("z_walk") == 0){ if(this->gethandState()==HandTypeLeft){ _armatureNode->getAnimation()->gotoAndPlay("z_skill_z"); }else if(this->gethandState()==HandTypeRight){ _armatureNode->getAnimation()->gotoAndPlay("z_skill_y"); }else{ _armatureNode->getAnimation()->gotoAndPlay("z_stand"); } }else if(eventData->animationState->name.compare("z_skill_z") == 0||eventData->animationState->name.compare("z_skill_y") == 0||eventData->animationState->name.compare("z_dizzy_over")==0){ _armatureNode->getAnimation()->gotoAndPlay("z_stand"); } }else{ if(eventData->animationState->name.compare("z_walk") == 0||eventData->animationState->name.compare("z_dizzy_over")==0){ _armatureNode->getAnimation()->gotoAndPlay("z_stand"); } if(eventData->animationState->name.compare("z_stand_e_z") == 0||eventData->animationState->name.compare("z_stand_e_y") == 0||eventData->animationState->name.compare("z_stand_e_ting_y") == 0||eventData->animationState->name.compare("z_stand_e_ting_z") == 0){ _armatureNode->getAnimation()->gotoAndPlay("z_stand"); this->setchattingState(ChatType0);//修改聊天状态 } if(eventData->animationState->name.compare("z_skill_start_b")==0){ _armatureNode->getAnimation()->gotoAndPlay("z_skill_start_c"); } } }break; case EventData::EventType::LOOP_COMPLETE:{ if(eventData->animationState->name.compare("z_stand_c")==0){ NotificationCenter::getInstance()->postNotification(Enemy_FALL_WATER,this); }else if(this->getEnemyState() != EnemyState2&&eventData->animationState->name.compare("z_target") != 0){ int index = rand()%10; if(index==1){ _armatureNode->getAnimation()->gotoAndPlay(stands[index]); }else{ _armatureNode->getAnimation()->gotoAndPlay(stands[0]); } } if(eventData->animationState->name.compare("z_skill_start_c")==0){ _armatureNode->getAnimation()->gotoAndPlay("z_skill_start_c"); } }break; } } }
原文链接:https://www.f2er.com/cocos2dx/340397.html

猜你在找的Cocos2d-x相关文章