接着上篇文章讲解如何调用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了。
这个更简单!!!
以上就是在三种平台上的调用过程!
如有不足之处,请大家指出,一起交流成长!