void
ArmatureNode::registerMovementEventHandler(
int
handler)
{
unregisterMovementEventHandler();
//移除之前注册的监听
_movementHandler = handler;
//缓存lua函数的引用 这个后边说
auto dispatcher = getCCEventDispatcher();
auto f = [
this
](cocos2d::EventCustom *event)
//注册c代码形式的回调 这里用function做
{
auto eventData = (dragonBones::EventData*)(event->getUserData());
auto type = (
int
) eventData->getType();
auto movementId = eventData->animationState->name;
auto lastState = eventData->armature->getAnimation()->getLastAnimationState();
auto stack = cocos2d::LuaEngine::getInstance()->getLuaStack();
stack->pushObject(
this
,
"db.ArmatureNode"
);
stack->pushInt(type);
stack->pushString(movementId.c_str(), movementId.size());
//通过LuaStack调用lua里的函数 最后一个参数设置参数个数
stack->executeFunctionByHandler(_movementHandler, 3);
};
dispatcher->addCustomEventListener(dragonBones::EventData::COMPLETE, f);
}
void
ArmatureNode::unregisterMovementEventHandler(
void
)
{
if
(0 != _movementHandler)
{
cocos2d::LuaEngine::getInstance()->removeScriptHandler(_movementHandler);
//移除lua函数的绑定
_movementHandler = 0;
}
}