见很多人发问cocos2dx 3+ 版本 lua 函数回调问题, 我在项目中是这样解决的:
因为我是使用了cocos 带有的 lua 绑定脚本(python写的): cocos2d-x/tools/tolua/genbindings.py
在生成绑定的cpp文件中 作出 一点修改就好了:
注释掉的那句是本来生成的代码 改用tolua_fix 中的 toluafix_ref_function 方法即可, 第二个参数 用 注释掉的那个函数调用的第二个参数。
1
2
3
|
LUA_FUNCTION arg0;
//ok &= luaval_to_int32(tolua_S, 2,(int *)&arg0, "TcpHelper:test");
arg0 = toluafix_ref_function(tolua_S, 2, 0);
|
下面是c++ 中的代码
1
2
3
4
5
6
7
8
|
void
test(LUA_FUNCTION listener){
LuaStack *stack = LuaEngine::getInstance()->getLuaStack();
stack->clean();
LuaValueDict dict;
dict[
"name"
] = LuaValue::stringValue(
"success"
);
stack->pushLuaValueDict(dict);
stack->executeFunctionByHandler(listener, 1);
}
|
lua 这里:
1
2
3
4
5
|
local
entity = TestClass:new()
entity:test(
function
(arg)
print
(
"!!!!"
)
dump(arg)
end
)
|
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)