1.在userpath\quick-cocos2d-x\lib\cocos2d-x\external\extra目录下,建立自定义导出类目录。
(我的目录为class)
2.在新建目录中添加自定义导出类,*.h文件和*.cpp文件。
#myMathEx.h文件
#ifndef _GAME_MYMATHEX_H_ #define _GAME_MYMATHEX_H_
class myMathEx { public: static int add(int a,int b); };
#endif
#myMathEx.cpp文件
#include "myMathEx.h" int myMathEx::add(int a,int b) { return a + b; }
3.在userpath\quick-cocos2d-x\lib\cocos2d-x\external\extra\luabinding目录下,新建cocos2dx_extra_user_luabinding.tolua文件,和User_build.bat文件。
#cocos2dx_extra_user_luabinding.tolua文件
$#include "Classes/myMathEx.h"
class myMathEx { static int add(int a,int b); };
#User_build.bat文件
@echo on set DIR=%~dp0 set OUTPUT_DIR=%DIR% set MAKE_LUABINDING="%QUICK_COCOS2DX_ROOT%\bin\compile_luabinding.bat" pushd cd "%DIR%" echo. call %MAKE_LUABINDING% -d %OUTPUT_DIR% cocos2dx_extra_user_luabinding.tolua pause popd
4.执行User_build.bat文件,会生成luabinding文件,你会在同级目录下看到cocos2dx_extra_user_luabinding.h,
cocos2dx_extra_user_luabinding.cpp
5.修改quick-cocos2d-x\lib\cocos2d-x\external目录下Android.mk文件,添加编译目录。
#Android.mk文件
......
LOCAL_SRC_FILES := \ extra/crypto/CCCrypto.cpp \ extra/crypto/base64/libbase64.c \ extra/network/CCHTTPRequest.cpp \ extra/network/CCNetwork.cpp \ extra/Classes/myMathEx.cpp \ //此处为添加编译目录 extra/platform/android/CCCryptoAndroid.cpp \ extra/platform/android/CCNativeAndroid.cpp \ extra/platform/android/CCNetworkAndroid.cpp \ extra/luabinding/cocos2dx_extra_luabinding.cpp \
......
6.将目录classes目录及其文件导入到工程 cocos2dx_2012\cocos2d-x\external\extra目录下。
7.将cocos2dx_extra_user_luabinding.h,cocos2dx_extra_user_luabinding.cpp文件导入到工程 extra\luabinding目录下。
8.添加工程的编译目录,【c/c++】【附件包含目录】中添加$(QUICK_COCOS2DX_ROOT)\lib\cocos2d-x\external\extra\Classes,编译工程cocos2dx_2012,重新生成cocos2dx.lib静态库。
备注:
生成的cocos2dx_extra_user_luabinding.cpp文件稍做修改:
需要添加两个头文件,#include "cocos2dx_extra_user_luabinding.h" #include "CCLuaEngine.h"
9...\quick-cocos2d-x\lib\cocos2d-x\scripting\lua\cocos2dx_support\CCLuaStack.cpp,文件添加相应的头文件。#include "cocos2dx_extra_user_luabinding.h"。在bool CCLuaStack::init(void)函数中添加lua对导出类的支持, luaopen_cocos2dx_extra_user_luabinding(m_state);
10.完成自定义类的导出,在lua中测试。