Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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. // Customcatlass.h
  2. #ifndef __CUSTOM__CLASS
  3. #define __CUSTOM__CLASS
  4. #include "cocos2d.h"
  5. namespace cocos2d {
  6. class Customcatlass : public cocos2d::ref
  7. {
  8. public:
  9. Customcatlass();
  10. ~Customcatlass();
  11. bool init();
  12. std::string HelloMsg();
  13. CREATE_FUNC(Customcatlass);
  14. };
  15. } //namespace cocos2d
  16. #endif // __CUSTOM__CLASS
复制代码
// Customcatlass.cpp
  • #include "Customcatlass.h"
  • USING_NS_Cc;
  • Customcatlass::Customcatlass(){
  • }
  • Customcatlass::~Customcatlass(){
  • bool Customcatlass::init(){
  • return true;
  • std::string Customcatlass::HelloMsg() {
  • return "Hello from Customcatlass::sayHello";
  • }
  • 2.4 添加cocos2dx_custom.ini文件 由于使用的是Cocos Framework,所以创建的cocos2dx_custom.ini路径是 /Applications/Cocos/frameworks/cocos2d-x-3.4/tools/tolua/(默认安装路径)。需要注意的是headers的路径要指向项目/frameworks/runtime-src/Classes/,这里我用了绝对路径,您需要修改为自己的路径。 复制代码
    [cocos2dx_custom]
  • # the prefix to be added to the generated functions. You might or might not use this in your own
  • # templates
  • prefix = cocos2dx_custom
  • # 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 -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
  • 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/my -I%(cocosdir)s/cocos/base -I%(cocosdir)s/cocos/platform/android
  • cocos_flags = -DANDROID
  • 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/cocos/my/Customcatlass.h
  • headers = /Users/Jacky/Workspace/CocosLuaBinding/frameworks/runtime-src/Classes/Customcatlass.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 = Customcatlass.*
  • # 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 =
  • # 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
  • 2.5 修改genbindings.py genbindings.py脚本是用来运行绑定的脚本。这里的路径是/Applications/Cocos/frameworks/cocos2d-x-3.4/tools/tolua/genbindings.py。 找到cmd_args,加入下面这行: 复制代码
    'cocos2dx_custom.ini' : ('cocos2dx_custom','lua_cocos2dx_custom'),\
    output_dir参数指定了绑定文件的输出目录,这里可以更改为Classes,也可以不改。这里就不改了。 cmd_args的其他行是引擎的脚本绑定,如果未做修改,可以删除以避免引擎文件的重复绑定,加快绑定速度。这里也不改了。 2.6 运行绑定脚本 打开终端,进入tolua目录,运行刚才修改的genbindings.py脚本。 复制代码
    cd /Applications/Cocos/frameworks/cocos2d-x-3.4/tools/tolua
  • ./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文件,添加头文件 复制代码
    include "lua_cocos2dx_custom.hpp"
    在static int lua_module_register(lua_State* L)添加注册函数 复制代码
    register_all_cocos2dx_custom(L);
    3.2 关闭COCOS_IDE_DEBUG_SUPPORT 打开Classes/ide-support/CodEIDESupport.h文件,修改 复制代码
    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()中添加如下测试代码 复制代码
    local customcatlass = Customcatlass:create()
  • local msg = customcatlass:HelloMsg()
  • release_print("customcatlass's msg is : " .. msg) --注意已经没有cclog了
  • 5.搞定,收工~

    大佬总结

    以上是大佬教程为你收集整理的Cocos如何绑定lua自定义类全部内容,希望文章能够帮你解决Cocos如何绑定lua自定义类所遇到的程序开发问题。

    如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

    本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
    如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。