1、簡介javascript
最近正在學習cocos2d中的lua遊戲開發,由於lua開發的熱更新特性,你們開發遊戲好像都會優先選擇lua做爲開發語言。html
可是遇到一個問題,用lua寫一些簡單的程序沒什麼問題,可是一旦須要一個複雜的類,在lua中直接寫就感受有些吃力。因此想到,能夠把遊戲開發中比較複雜的模塊使用c++完成,而後導出到lua,讓lua能夠輕鬆調用。java
我從頭至尾完整地完成了cocos2dx-3.x中自定義類的導出過程,在網上查了好多資料,也碰到了不少錯誤,然而網上關於這塊的文章比較零散,若是有初學者使用的話得費半天勁才能找全。因此我在這篇文章中詳細地介紹整個過程,並將過程當中容易出現的問題和解決方法列舉出來,供你們參考。python
2、過程詳解android
2.1 環境配置(windows環境下)c++
2.2 寫C++類git
我測試用的是cocos2d-x-3.3\tests\lua-empt-test\project\Classes\HelloWorldScene.cpp。github
2.3 寫一個導出的python腳本express
1)進入目錄cocos2d-x-3.3\tools\tolua,複製一份genbindings.py,命名爲genbindings_myclass.py 2)把生成目錄制定到咱工程裏去,打開genbindings_myclass.py把windows
1
|
output_dir =
'%s/cocos/scripting/lua-bindings/auto'
% project_root
|
改爲
1
|
output_dir =
'%s/tests/lua-empty-test/project/Classes/auto'
% project_root
|
3)修改命令參數,把
1
2
3
4
5
6
7
|
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'
), \
}
|
改爲
1
|
cmd_args
=
{
'myclass.ini'
: (
'myclass'
,
'lua_myclass_auto'
) }
|
2.4 寫myclass.ini文件
咱們接下來寫myclass.ini文件,拷貝一份cocos2dx_spine.ini更名,修改爲以下內容:
[myclass]
# the prefix to be added to the generated functions. 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 that namespace
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/tests/lua-empty-test/project/Classes/HelloWorldScene.h
# what classes to produce code for. You can use regular expressions here. When testing the regular
# expression, it will be enclosed in "^$", like this: "^Menu*$".
classes = HelloWorld
# what should we skip? in the format ClassName::[function function]
# ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also
# regular expressions, they will not be surrounded 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 a convenience wildcard to be able to skip similar named
# functions from all classes.
skip =
rename_functions =
rename_classes =
# for all class names, should we remove something 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 when their sub-classes found them.
base_classes_to_skip = Ref ProcessBase
# classes that create no constructor
# Set is special and we will use a hand-written constructor
abstract_classes =
# Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'.
script_control_cpp = no
改的時候要注意這些行:
1
2
3
4
5
6
7
|
[myclass]
prefix
=
myclass
target_namespace
=
headers
=
%
(cocosdir)s
/
tests
/
lua
-
empty
-
test
/
project
/
Classes
/
HelloWorldScene.h
classes
=
HelloWorld
skip
=
abstract_classes
=
|
2.5 運行python腳本
下面要自動生成代碼了,打開命令行工具,cd到cocos2d-x-3.3\tools\tolua下,敲入
1
|
python genbindings_myclass.py
|
回車運行。若是前面沒問題的話你會在cocos2d-x-3.3\tests\lua-empty-test\project\Classes多了一個文件夾auto。
若是生成成功,那麼恭喜你已經越過了難度最大、陷阱最多的地方。若是出現錯誤,那麼極可能是你第一部分的環境配置有問題。
注意:若是出現錯誤「dos2unix 既不是內部或外部命令,也不是可運行的程序」這樣的錯誤,個人解決方法是,把本身從網上下載的dos2unix.exe文件放在C:\Windows\System32這個文件夾底下,再從新運行腳本,就發現錯誤沒有了。若是還不行,能夠配置環境變量裏的path,使之指向C:\Windows\System32目錄。dos2unix.exe的下載目錄:http://pan.baidu.com/s/1kTghHzD。
2.6 把自定義類的module加入lua
而後把裏面生成lua_myclass_auto.cpp和lua_myclass_auto.hpp加入工程中。
把咱們生成的個module在腳本引擎初始化的時候加入lua。編輯AppDelegate.cpp,包含lua_myclass_auto.hpp頭文件,在
1
|
LuaEngine* engine = LuaEngine::getInstance();
|
後面加入
1
|
register_all_myclass(engine->getLuaStack()->getLuaState());
|
2.7 編譯運行
編譯運行。這樣HelloWorld這個類就被導出到lua了。
2.8 導出成功,在lua中使用 這樣整個導出過程就完成了,咱們就能夠在lua中使用自定義類了。
這樣咱們就大功告成了!