接着上篇文章講解如何調用C++,網上是有很多文章說如何調用,但就是沒有具體步驟,要麼簡單說兩句了事,要麼是Mac上的,搞得我是win32上的又是一陣蛋疼。。。但是大體步驟還是一樣的,小弟愚昧,試了很久才成功,現在分享下!
本篇文章會說在三種調用:win32,player,真機。
再次聲明,以下說的frameworks都是項目下的frameworks,不是quick下的!!!!
先說修改AppDelegate.cpp文件的步驟,因爲不管在哪種調用都是要修改AppDelegate.cpp文件,只是修改不同項目的AppDelegate.cpp!!!
修改AppDelegate.cpp文件
1.添加頭文件:#include "lua_cocos2dx_custom_auto.hpp"
2.調用register_all_cocos2dx_custom(L);在下面位置調用,並確定Lua的棧頂元素是_G。
stack->setXXTEAKeyAndSign("2dxLua", strlen("2dxLua"), "XXTEA", strlen("XXTEA")); //register custom function //LuaStack* stack = engine->getLuaStack(); //register_custom_function(stack->getLuaState()); engine->getLuaStack()->loadChunksFromZIP("res/framework_quick.zip"); { lua_State *L = stack->getLuaState(); lua_getglobal(L, "_G"); register_all_cocos2dx_custom(L); lua_settop(L, 0); } #if (COCOS2D_DEBUG > 0) // NOTE:Please don't remove this call if you want to debug with Cocos Code IDE if (_launchMode) { startRuntime(); } else { engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str()); } #else engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str()); #endif
要在COCOS2D_DEBUG之前調用!!!!
3.在項目main.lua裏調用foo函數
local test = cc.CustomClass:create() printInfo("lua bind: " .. test:foo(99))
會看到以下效果:
官網文檔就講到第三步,沒說怎麼實現以上效果,把我個給坑死了!!!!
在win32上調用
在frameworks/runtime-src/Classes/目錄下,修改AppDelegate.cpp文件。
如果要運行frameworks/runtime-src/proj.win32的項目,達到效果要麻煩點。
1.把CustomClass 和 lua_cocos2dx_custom_auto 類複製到frameworks/runtime-src/Classes/目錄下就行,只要能在編譯的時候找到!!
2.添加到項目裏
3.要在項目的根目錄下(例如我的項目叫test,那就在test目錄下),創建Resources文件夾,該文件夾裏是項目的根目錄下的res文件夾和src文件夾,不然運行的時候會找不到文件!!!這樣就可以運行成功了!
4.修改AppDelegate.cpp文件
最難的解決了,以下兩個就簡單點了!
在Player上調用
1. 把CustomClass 和 lua_cocos2dx_custom_auto 類複製到/quick/player/Classes目錄下(也就是palyer項目,3.3rc1把player項目放出來了)
2.把CustomClass 和 lua_cocos2dx_custom_auto 類添加到player項目裏
3.修改/quick/player/Classes目錄下AppDelegate.cpp文件。
是不是很簡單!!!!
在真機上調用
1. 把CustomClass 和 lua_cocos2dx_custom_auto 類複製到frameworks/runtime-src/Classes/目錄下。
2.修改frameworks/runtime-src/Classes/目錄下AppDelegate.cpp文件,然後按真機編譯步驟進行就OK了。
這個更簡單!!!
以上就是在三種平臺上的調用過程!
如有不足之處,請大家指出,一起交流成長!