Cocos2dx(3.3)绑定lua
Cocos2dx(3.3)绑定lua 参考:http://blog.sina.com.cn/s/blog_693de6100101ocg4.html http://segmentfault.com/blog/hongliang/1190000000631630 1、安装必要的库和工具包,以及配置相关环境变量,请按照cocos2d-x-3.0rc0toolstoluaREADME.mdown说得去做,不赘述。 绑定lua必须安装的两个软件PyYAML、Cheetah这两个软件 2、写c++类(我测试用的是Mytestl1/frameworks/runtime-src/Classes/HelloWorldScene.cpp) 3、写一个生成的python脚本,你不会写,没关系,我们会照猫画虎 1)进入目录Mytestl1/frameworks/cocos2d-x/tools/tolua,复制一份genbindings.py,命名为genbindings_myclass.py 2)把生成目录制定到咱工程里去,打开genbindings_myclass.py把
---------------------------------------------- output_dir = '%s/cocos/scripting/lua-bindings/auto'% project_root ---------------------------------------------- 改成 ---------------------------------------------- output_dir = '%s/../runtime-src/Classes/auto' %project_root ---------------------------------------------- 3)修改命令参数,把 ---------------------------------------------- cmd_args = {'cocos2dx.ini' : ('cocos2d-x','lua_cocos2dx_auto'), 'cocos2dx_extension.ini' :('cocos2dx_extension','lua_cocos2dx_extension_auto'), 'cocos2dx_ui.ini' : ('cocos2dx_ui','lua_cocos2dx_ui_auto'), 'cocos2dx_studio.ini' :('cocos2dx_studio','lua_cocos2dx_studio_auto'), 'cocos2dx_spine.ini' :('cocos2dx_spine','lua_cocos2dx_spine_auto'), 'cocos2dx_physics.ini' :('cocos2dx_physics','lua_cocos2dx_physics_auto'), } ---------------------------------------------- 改成 ---------------------------------------------- cmd_args = {'myclass.ini' : ('myclass','lua_myclass_auto') } 4)这时你可能问myclass.ini在哪啊,我们下来就写这个文件。原理一样,我还是照猫画虎,拿cocos2dx_spine.ini改的。 [myclass] # the prefix to be added to the generatedfunctions. You might or might not use this in your own # templates prefix = myclass # create a target namespace (in javascript,this would create some code like the equiv. to `ns = ns || {}`) # all classes will be embedded in thatnamespace target_namespace =
android_headers =-I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include-I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include-I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/include android_flags = -D_SIZE_T_DEFINED_
clang_headers =-I%(clangllvmdir)s/lib/clang/3.3/include clang_flags = -nostdinc -x c++ -std=c++11
cocos_headers = -I%(cocosdir)s/cocos-I%(cocosdir)s/cocos/2d -I%(cocosdir)s/cocos/base -I%(cocosdir)s/cocos/ui-I%(cocosdir)s/cocos/physics -I%(cocosdir)s/cocos/2d/platform-I%(cocosdir)s/cocos/2d/platform/android -I%(cocosdir)s/cocos/math/kazmath-I%(cocosdir)s/extensions -I%(cocosdir)s/external-I%(cocosdir)s/cocos/editor-support -I%(cocosdir)s
cocos_flags = -DANDROID-DCOCOS2D_JAVASCRIPT
cxxgenerator_headers =
# extra arguments for clang extra_arguments = %(android_headers)s%(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(android_flags)s%(clang_flags)s %(cocos_flags)s %(extra_flags)s
# what headers to parse headers =%(cocosdir)s/../runtime-src/Classes/HelloWorldScene.h # what classes to produce code for. You canuse regular expressions here. When testing the regular # expression,it will be enclosed in"^$",like this: "^Menu*$". classes = HelloWorld
# what should we skip? in the formatClassName::[function function] # ClassName is a regular expression,butwill be used like this: "^ClassName$" functions are also # regular expressions,they will not besurrounded by "^$". If you want to skip a whole class,just # add a single "*" as functions.See bellow for several examples. A special class name is "*",which # will apply to all class names. This is aconvenience wildcard to be able to skip similar named # functions from all classes.
skip =
rename_functions =
rename_classes =
# for all class names,should we removesomething when registering in the target VM? remove_prefix =
# classes for which there will be no"parent" lookup classes_have_no_parents =
# base classes which will be skipped whentheir sub-classes found them. base_classes_to_skip = Ref ProcessBase
# classes that create no constructor # Set is special and we will use ahand-written constructor abstract_classes =
# Determining whether to use scriptobject(js object) to control the lifecycle of native(cpp) object or the otherway around. Supported values are 'yes' or 'no'. script_control_cpp = no ---------------------------------------------- 改的时候要注意这些行 ---------------------------------------------- [myclass] prefix = myclass target_namespace = headers =%(cocosdir)s/../runtime-src/Classes/HelloWorldScene.h classes = HelloWorld skip = abstract_classes = ---------------------------------------------- headers这个路径一定要引用正确,否则编译的时候会报错 4、下面要自动生成代码了,打开命令行工具,cd到Mytestl1/frameworks/cocos2d-x/tools/tolua下,敲入 ---------------------------------------------- python genbindings_myclass.py ---------------------------------------------- 回车运行。如果前面没问题的话你会在Mytestl1/frameworks/runtime-src/Classes多了一个文件夹auto,然后把里面生成lua_myclass_auto.cpp和lua_myclass_auto.hpp加入拽如工程 5、把我们生成的个module在脚本引擎初始化的时候加入lua。 编辑AppDelegate.cpp,包含lua_myclass_auto.hpp头文件,在 ---------------------------------------------- LuaEngine* engine =LuaEngine::getInstance(); ---------------------------------------------- 后面加入 ---------------------------------------------- register_all_myclass(engine->getLuaStack()->getLuaState()); 一定要在AppDelegate.cpp文件中包含lua_myclass_auto.hpp头文件 ---------------------------------------------- 6、编译运行。这样HelloWorld这个类就被导出到lua了。 测试------------- 打开hello.lua,编辑local function main()这个函数 把前面改成 ---------------------------------------------- local function main() -- avoid memory leak collectgarbage("setpause",100) collectgarbage("setstepmul",5000) local hello = HelloWorld:create() local sceneGame = cc.Scene:create() sceneGame:addChild(hello) cc.Director:getInstance():runWithScene(sceneGame) if(1==1) then return end …… …… ---------------------------------------------- 关于lua绑定中遇到的一些坑 1、 在编写genbindings_myclass.py文件的时候,应用NDK_ROOT路径的问题, 直接在文件中写成 try: NDK_ROOT = os.environ['NDK_ROOT'] NDK_ROOT = /Users/zctech_10/Tools/adt-bundle-mac-x86_64-20130917/android-ndk-r9b except Exception: print "NDK_ROOT not defined. Please define NDK_ROOT in yourenvironment." sys.exit(1) return NDK_ROOT 或者你可以在终端里边执行export /Users/zctech_10/Tools/adt-bundle-mac-x86_64-20130917/android-ndk-r9b命令 这里一定要使用android-ndk-r9b,而不是android-ndk-r9 2、在编写genbindings_myclass.py文件的时候,写关于output_dir路径的坑 output_dir= '%s/../runtime-src/Classes/auto' %project_root 这个要写你自己的Classes的路径(或许你可以写成其他的路径,暂时还没试验其他的路径是否ok) 3、在编写myclass.ini文件的时候, headers= %(cocosdir)s/../runtime-src/Classes/HelloWorldScene.h 一定要写的是HelloWorldScene.h文件的路径,不然就会报错,如果你需要绑定的文件是其他的文件,你就写你自己需要绑定的文件 4、第四个坑就是绑定成功了,但是一运行就会报错 修改如下: AppDelegate.cpp文件中修改 lua_State *L =stack->getLuaState(); lua_getglobal(L,"_G"); register_all_MyClass(L); lua_settop(L,0); 重新编译并执行,程序就正确执行了: 注意代码的顺序
关于绑定lua 遇到的一些坑 没用的头文件要去掉 不能有cc_ex_dll 名字空间转换在conversions.yaml设置 每个类名都要写 安装 Cheetah-2.4.4PyYAML-3.11 sudo python ./setup.py install (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |