cocos2d-x3.2里lua文件,调用C++的接口

把applicationDidFinishLaunching函数加成这样,在加个函数mytestlua

int mytestlua(lua_State *L)
{
	int number = lua_tonumber(L, 1);

	number = number + 1;

	lua_pushnumber(L, number);

	return 1;
}

bool AppDelegate::applicationDidFinishLaunching()
{
    auto engine = LuaEngine::getInstance();
    ScriptEngineManager::getInstance()->setScriptEngine(engine);

	LuaStack* stack = engine->getLuaStack();
	stack->setXXTEAKeyAndSign("2dxLua", strlen("2dxLua"), "XXTEA", strlen("XXTEA"));
	
	lua_State *L = stack->getLuaState();
	lua_register(L, "mytestlua", mytestlua);

    if (engine->executeScriptFile("src/main.lua")) {
        return false;
    }

    return true;
}
 
 
main.lua 加一下这个打印的
<pre class="cpp" name="code">    local i = mytestlua(88)
    cclog("lua bind: " .. tostring(i))

 
 



你可能感兴趣的:(cocos2d-x3.2里lua文件,调用C++的接口)