CocosStudio导出序列帧功能3.x其实用起来已经挺方便的。不过很多人使用的时候都发现,在lua中播完序列帧回调setLastFrameCallFunc无法使用,网上也有很多人说setFrameEventCallFunc来处理,但是这种每帧都回调明显不靠谱,而且笔者用的3.6也发现ActionTimeline并没用调用emitFrameEvent(其实这个是CCFrame中Frame::emitEvent的实现),所以也不存在回调。
为什么setLastFrameCallFunc会直接抛出断言,看源码lua_cocos2dx_studio_auto.cpp可以发现方法其实没用调用。
do {
// Lambda binding for lua is not supported.
assert(false);
} while(0)
可以看到bindings-generator自动生成时,lambda function 用来做函数的参数是无法导出的(c++11 的 functional也是如此)!
解决方法很简单,只需要在lua_cocos2dx_coco_studio_manual.cpp中手动导出一遍setLastFrameCallFunc。不太懂lua绑定的童鞋可以参考其中手动导出setFrameEventCallFunc的做法。
下面贴一下我的lua-binding,有需要的就带走
static int lua_cocos2dx_studio_ActionTimeline_setLastFrameCallFunc(lua_State* L)
{
if (nullptr == L)
return 0;
int argc = 0;
cocostudio::timeline::ActionTimeline* self = nullptr;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
if (!tolua_isusertype(L,1,"ccs.ActionTimeline",0,&tolua_err)) goto tolua_lerror;
#endif
self = static_cast(tolua_tousertype(L,1,0));
#if COCOS2D_DEBUG >= 1
if (nullptr == self) {
tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_studio_ActionTimeline_setLastFrameCallFunc'\n", NULL);
return 0;
}
#endif
argc = lua_gettop(L) - 1;
if (1 == argc)
{
#if COCOS2D_DEBUG >= 1
if (!toluafix_isfunction(L,2,"LUA_FUNCTION",0,&tolua_err) )
{
goto tolua_lerror;
}
#endif
LUA_FUNCTION handler = ( toluafix_ref_function(L,2,0));
self->setLastFrameCallFunc([=](void){
LuaEngine::getInstance()->getLuaStack()->executeFunctionByHandler(handler, 0);
});
return 0;
}
luaL_error(L, "'setLastFrameCallFunc' function of ActionTimeline has wrong number of arguments: %d, was expecting %d\n", argc, 1);
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(L,"#ferror in function 'setLastFrameCallFunc'.",&tolua_err);
#endif
return 0;
}
extendActionTimeline中记得增加方法导出
tolua_function(L, "setLastFrameCallFunc", lua_cocos2dx_studio_ActionTimeline_setLastFrameCallFunc);
编译之后,回调可用