最近的項目開始用CocoStudio來創建骨骼動畫,在使用過程中發現CocoStudio中的骨骼動畫還是挺好用的,在用的過程中發現有幾個地方還是要比較注意的
骨骼動畫的幀回調
我在使用中,一開始找這個回調設置的地方搞了很久,後面才發現如果要代碼中要有幀回調,那麼在界面的幀事件上一定要填寫一下名稱(如圖):
就是這個幀事件就一定要填東西;
代碼:
armature = Armature::create("Cowboy");
armature->getAnimation()->play("FireWithoutBullet");
armature->getAnimation()->setSpeedScale(0.2f);
armature->setScaleX(-0.2f);
armature->setScaleY(0.2f);
armature->setPosition(Point(VisibleRect::left().x + 70, VisibleRect::left().y));
/*
* Set armature's frame event callback function
* To disconnect this event, just setFrameEventCallFunc(nullptr);
*/
armature->getAnimation()->setFrameEventCallFunc(CC_CALLBACK_0(TestColliderDetector::onFrameEvent, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
addChild(armature);
void TestColliderDetector::onFrameEvent(Bone *bone, const std::string& evt, int originFrameIndex, int currentFrameIndex)
{
CCLOG("(%s) emit a frame event (%s) at frame index (%d).", bone->getName().c_str(), evt.c_str(), currentFrameIndex);
/*
* originFrameIndex is the frame index editted in Action Editor
* currentFrameIndex is the current index animation played to
* frame event may be delay emit, so originFrameIndex may be different from currentFrameIndex.
*/
Point p = armature->getBone("Layer126")->getDisplayRenderNode()->convertToWorldSpaceAR(Point(0, 0));
bullet->setPosition(Point(p.x + 60, p.y));
bullet->stopAllActions();
bullet->runAction(CCMoveBy::create(1.5f, Point(350, 0)));
}
這個動畫就是很好的回調了