最近的项目开始用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)));
}
这个动画就是很好的回调了