Cocos如何绑定lua自定义类

从论坛和技术支持的反馈来看,很多童鞋在Cocos中进行lua绑定时遇到了不少问题,因为Cocos使用的是预编译库的方式,所以和之前用Cocos2d-x绑定又不太一样。原有的教程也需要做调整,这篇文章目的是在Cocos中创建一个自定义类,并进行lua绑定,然后在lua中调用C++。  
 
关于如何在Cocos2d-x中进行lua绑定自定义类,请看 Binding_Custom_Class_To_Lua_Runtime 。本文是基于此教程做的调整。  
 
1.版本说明  
 
环境: Mac OSX  
 
Cocos: v2.1  
 
Cocos2d-x: v3.4Final  
 
Cocos Code IDE: v1.2.0  
 
Cocos Studio: v2.1.2 beta(这里没用到)  
 
2.在Cocos中进行lua绑定自定义类  
 
2.1 创建项目  
 
首先,创建一个Cocos项目,名字为CocosLuaBinding。  
 
2.2 发布项目  
 
创建后默认会使用Cocos Studio打开,选择发布为XCode工程。  
 
2.3 添加自定义类  
 
在XCode中,项目的Classes目录下添加Custom类。  
 
复制代码
  1. // CustomClass.h
  2. #ifndef __CUSTOM__CLASS
  3. #define __CUSTOM__CLASS
  4. #include "cocos2d.h"
  5. namespace cocos2d {
  6. class CustomClass : public cocos2d::Ref
  7. {
  8. public:
  9.     CustomClass();
  10.     ~CustomClass();
  11.     bool init();
  12.     std::string helloMsg();
  13.     CREATE_FUNC(CustomClass);
  14. };
  15. } //namespace cocos2d
  16. #endif // __CUSTOM__CLASS
 
复制代码
  1. // CustomClass.cpp
  2. #include "CustomClass.h"
  3. USING_NS_CC;
  4. CustomClass::CustomClass(){
  5. }
  6. CustomClass::~CustomClass(){
  7. }
  8. bool CustomClass::init(){
  9.     return true;
  10. }
  11. std::string CustomClass::helloMsg() {
  12.     return "Hello from CustomClass::sayHello";
  13. }
 
2.4 添加cocos2dx_custom.ini文件  
 
由于使用的是Cocos Framework,所以创建的cocos2dx_custom.ini路径是 /Applications/Cocos/frameworks/cocos2d-x-3.4/tools/tolua/(默认安装路径)。需要注意的是headers的路径要指向项目/frameworks/runtime-src/Classes/,这里我用了绝对路径,您需要修改为自己的路径。  
 
复制代码
  1. [cocos2dx_custom]
  2. # the prefix to be added to the generated functions. You might or might not use this in your own
  3. # templates
  4. prefix = cocos2dx_custom
  5. # create a target namespace (in javascript, this would create some code like the equiv. to `ns = ns || {}`)
  6. # all classes will be embedded in that namespace
  7. target_namespace =
  8. 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 -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/include
  9. android_flags = -D_SIZE_T_DEFINED_
  10. clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include
  11. clang_flags = -nostdinc -x c++ -std=c++11
  12. cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/my -I%(cocosdir)s/cocos/base -I%(cocosdir)s/cocos/platform/android
  13. cocos_flags = -DANDROID
  14. cxxgenerator_headers =
  15. # extra arguments for clang
  16. 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
  17. # what headers to parse
  18. #headers = %(cocosdir)s/cocos/my/CustomClass.h
  19. headers = /Users/Jacky/Workspace/CocosLuaBinding/frameworks/runtime-src/Classes/CustomClass.h
  20. # what classes to produce code for. You can use regular expressions here. When testing the regular
  21. # expression, it will be enclosed in "^$", like this: "^Menu*$".
  22. classes = CustomClass.*
  23. # what should we skip? in the format ClassName::[function function]
  24. # ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also
  25. # regular expressions, they will not be surrounded by "^$". If you want to skip a whole class, just
  26. # add a single "*" as functions. See bellow for several examples. A special class name is "*", which
  27. # will apply to all class names. This is a convenience wildcard to be able to skip similar named
  28. # functions from all classes.
  29. skip =
  30. rename_functions =
  31. rename_classes =
  32. # for all class names, should we remove something when registering in the target VM?
  33. remove_prefix =
  34. # classes for which there will be no "parent" lookup
  35. classes_have_no_parents =
  36. # base classes which will be skipped when their sub-classes found them.
  37. base_classes_to_skip =
  38. # classes that create no constructor
  39. # Set is special and we will use a hand-written constructor
  40. abstract_classes =
  41. # 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'.
  42. script_control_cpp = no
 
2.5 修改genbindings.py  
 
genbindings.py脚本是用来运行绑定的脚本。这里的路径是/Applications/Cocos/frameworks/cocos2d-x-3.4/tools/tolua/genbindings.py。  
 
找到cmd_args,加入下面这行:  
 
复制代码
  1.   'cocos2dx_custom.ini' : ('cocos2dx_custom', 'lua_cocos2dx_custom'), \
 
output_dir参数指定了绑定文件的输出目录,这里可以更改为Classes,也可以不改。这里就不改了。  
 
cmd_args的其他行是引擎的脚本绑定,如果未做修改,可以删除以避免引擎文件的重复绑定,加快绑定速度。这里也不改了。  
 
2.6 运行绑定脚本  
 
打开终端,进入tolua目录,运行刚才修改的genbindings.py脚本。  
 
复制代码
  1. cd /Applications/Cocos/frameworks/cocos2d-x-3.4/tools/tolua
  2. ./genbindings.py
 
这里不讨论绑定失败的情况,大部分是环境变量没配好的原因,在运行之前请先阅读/Applications/Cocos/frameworks/cocos2d-x-3.4/tools/tolua/README.mdown。  
 
2.7 拷贝绑定文件到项目目录  
 
在成功运行完绑定脚本后,绑定文件会生成在/Applications/Cocos/frameworks/cocos2d-x-3.4/cocos/scripting/lua-bindings/auto/lua_cocos2dx_custom.cpp(h)  
 
将这俩个文件剪切到Classes目录,并添加到XCode项目中。  
 
到此为止,基本上是和Binding_Custom_Class_To_Lua_Runtime一致的。  
 
3.编译运行  
 
 
由于使用的是Cocos Framework,所以引擎没有源码了,只有预编译库,这也就是为什么在第二步中我们主要做的调整:把自定义类及其绑定文件放到Classes中。  
 
3.1 注册自定义类  
 
打开Classes/lua_module_register.h文件,添加头文件  
 
复制代码
  1. include "lua_cocos2dx_custom.hpp"
 
 
在static int lua_module_register(lua_State* L)添加注册函数  
 
复制代码
  1. register_all_cocos2dx_custom(L);
 
3.2 关闭COCOS_IDE_DEBUG_SUPPORT  
 
打开Classes/ide-support/CodeIDESupport.h文件,修改  
 
复制代码
  1. define CC_CODE_IDE_DEBUG_SUPPORT 0
 
 
3.3 在Cocos IDE中构建自定义模拟器  
 
在Cocos IDE中打开项目,选中CocosLuaBinding项目,右键,Cocos工具,构建自定义模拟器,选择相应的模拟器,点击生成。  
 
因为Cocos Framework默认使用自带的模拟器,所以如果修改了C++文件,仍然使用默认模拟器,修改的C++文件不会生效。  
 
构建自定义模拟器后,以后每次都会使用自定义的模拟器。  
 
3.4 如何知道目前使用的是自定义模拟器还是默认模拟器?  
 
选中CocosLuaBinding项目,右键,调试方式,调试配置。  
 
以Mac平台为例:  
 
自定义模拟器路径为/Users/Jacky/Workspace/CocosLuaBinding/runtime/mac/CocosLuaBinding Mac  
 
默认模拟器路径为/Applications/Cocos/cocos-simulator-bin/mac/Simulator.app  
 
4.测试绑定是否成功  
 
在src/app/views/MainScene.lua的function MainScene:onCreate()中添加如下测试代码  
 
复制代码
  1. local customClass = CustomClass:create()
  2.     local msg = customClass:helloMsg()
  3.     release_print("customClass's msg is : " .. msg) --注意已经没有cclog了
 
 
5.搞定,收工~  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值