Please respect the original reprint please famous source: http://blog.csdn.net/wushao126/article/details/46660375
First go to the official website to download the latest quick, configure the environment, create a LUA project, command Cocos New XXX (project name)-P com.xxxx.xxxx (package name)-L lua-d XXX (new Project Save path)
After the project is created, locate the frameworks\runtime-src\classes in the project directory, create a C + + class, and the class I created is
File name: LuaBindingTest.h
#pragma onceclass luabindingtest{public:luabindingtest (); ~luabindingtest (); void Add (long a,long b); void sub (int a, int b);};
File name: LuaBindingTest.cpp
#include "LuaBindingTest.h" #include "cocos2d.h" USING_NS_CC; Luabindingtest::luabindingtest () {}luabindingtest::~luabindingtest () {}void luabindingtest::sub (int a, int b) {CCLOG ( "A-B =%d",-a);} void Luabindingtest::add (Long A, long B) {Cclog ("a + b =%ld", A + b);}
After that, locate the genbindings.py folder in the Frameworks\cocos2d-x\tools\tolua folder in the project directory, back up a copy, change the name Genbindings_binding_ test.py, open this file, you need to modify the content, positioning about 130 lines, find content similar
Tolua_root = '%s/tools/tolua '% project_root output_dir = '%s/'. /runtime-src/classes/auto '% project_root Cmd_args = {' Myclass.ini ': (' MyClass ', ' Lua_myclass_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_coco S2dx_studio_auto '), ' Cocos2dx_spine.ini ': (' cocos2dx_spine ', ' Lua_cocos2dx_spine_auto '), ' Cocos2dx_physics.ini ': (' cocos2dx_physics ', ' Lua_cocos2dx_physics_auto '), ' cocos2dx_experime Ntal_video.ini ': (' cocos2dx_experimental_video ', ' Lua_cocos2dx_experimental_video_auto '), ' cocos2dx_e Xperimental.ini ': (' cocos2dx_experimental ', ' Lua_cocos2dx_experimental_auto '), ' cocos2dx_controller.i Ni ': (' Cocos2dx_controller ', ' Lua_cocos2dx_controller_auto '), ' Cocos2dx_cocosbuilder.ini ': (' cocos2dx_cocosbuilder ', ' Lua_cocos2dx_cocosbuilder_auto '), ' Cocos2dx_cocosdenshion.ini ': (' cocos2dx_cocosdenshion ', ' Lua_cocos2dx_cocosdenshion_auto '), ' Coco S2dx_3d.ini ': (' cocos2dx_3d ', ' Lua_cocos2dx_3d_auto '), ' Cocos2dx_audioengine.ini ': (' Cocos2dx_audioeng Ine ', ' Lua_cocos2dx_audioengine_auto '), ' Cocos2dx_csloader.ini ': (' cocos2dx_csloader ', ' lua_cocos2dx_ Csloader_auto '),}Tolua_root is the directory where the INI file is located, we will create our INI file in this directory.
Output_dir is the file directory where we want to build the target file, and I'll change this in the Classes directory auto
This is the content of my modified file
Tolua_root = '%s/tools/tolua '% project_root output_dir = '%s/'. /runtime-src/classes/auto '% project_root Cmd_args = {' Myclass.ini ': (' MyClass ', ' Lua_myclass_auto '), }
Myclass.ini is the INI file to be created next
Randomly copy an INI file, rename to Myclass.ini, open the file, modify a few parameters can be
prefix = MyClass
Target_namespace = (empty indicates no)
headers =% (Cocosdir) s/: /runtime-src/classes/luabindingtest.h (requires binding the header file path of the C + + Class)
Classes = Luabindingtest (class name)
Skip = (interface hidden from LUA)
Save the file after you have modified it. Then you configure the environment
Locate the Readme.mdown file under this directory to open
Follow the above installation, do well, about cheetah this, directly copy the folder, do not enter the directory to put all the files scattered into the C:\Python27\Lib\site-packages
The NDK r9b version is better than that.
When the environment is well configured
Execute the Python genbindings_binding_test.py command
You will see the generated files, API folders, a HPP, a CPP on your output_out path. If the BAA generated, it is necessary to check whether the error link.
Registered
Open AppDelegate.cpp, add hpp header file
Appdelegate::applicationdidfinishlaunching method, under Lua_module_register (l), copy the HPP just generated in Register_all_myclass (L);
This is a good sign.
Call
Local p = luabindingtest.new (0)
P:add (4,1)
P:sub (2,1)
Over
Copyright NOTICE: This article is the original article of Bo Master, reprint please famous source.
COCOS2DX Lua Learning Notes < a > Quick 3.5 binding Custom C + + classes to Lua