cocos2dx3.10 lua 绑定C++ 类(mac xcode)

1 前期准备:需要安装好这两个东西,不然跑脚本的时候会报错

(1)  yaml

http://codyaray.com/2011/12/pyyaml-using-easy_install-on-mac-os-x-lion

1.报错

    ImportError: No module named yaml

2.安装

    sudo easy_install  pyyaml

error

   执行:sudo python -m easy_install pyyaml

   执行:sudo easy_install  pyyaml

(2)  Cheetah

    下载cheetah:http://pythonhosted.org//Cheetah/

    进入目录执行  sudo python setup.py install

2 先创建C++类 MyTestBind

MyTestBind.hpp

//
//  MyTestBind.hpp
//  firstCocosGame
//
//  Created by on 2018/8/27.
//

#ifndef MyTestBind_hpp
#define MyTestBind_hpp

#include <stdio.h>

#include "cocos2d.h"
#include<math.h>
using namespace cocos2d;
class MyTestBind : public Ref
{
public:
    //MyTestBind() {}
    //~MyTestBind() {}
    bool init()
    {
        return true;
    };
    CREATE_FUNC(MyTestBind);
    static MyTestBind* getInstance();
    void doTestLog();
    
    int fooTest(int a);
    int addTest(int a, int b);
};

#endif /* MyTestBind_hpp */

MyTestBind.cpp

//
//  MyTestBind.cpp
//  firstCocosGame
//
//  Created by  on 2018/8/27.
//

#include "MyTestBind.hpp"

void MyTestBind::doTestLog()
{
    CCLOG("MyTestBind 绑定成功!");
}

MyTestBind* MyTestBind::getInstance()
{
    static MyTestBind * instance;
    if(!instance)
    {
        instance = new MyTestBind();
    }
    return instance;
}

int MyTestBind::fooTest(int a)
{
    return a + 100;
}

int MyTestBind::addTest(int a, int b)
{
    return a + b;
}

3绑定步骤:

         1、绑定基本变量:这个按照readme中的去做,注意NDK要用:r9b一定要用这个,不然会有各种问题。。。。

          2、编译自己的ini文件。这个需要参照其他的ini。我参考的是cocos2dx_physics.ini

      路径:/Users/******/workGame/testGame/firstCocosGame/frameworks/cocos2d-x/tools/tolua

         自己创建一个文本文件将cocos2dx_physics.ini中的内容全部粘过来,之后我们只需要修改几个地方。

         下面是要修改的地方:

[mytestbind]  
prefix = mytestbind  
target_namespace =  
headers = /Users/*****/workGame/testGame/firstCocosGame/frameworks/runtime-src/proj.ios_mac/MyTestBind.hpp  --这里我用的是绝对路径用,如果用之前的变量路径找不到 MyTestBind.hpp
classes = MyTestBind  
skip =  
abstract_classes = 

  命名为mytestbind.ini

   3我们再自己写一个testgenbindings.py脚本这个脚本就是在genbindings.py这个脚本的基础上改的。copy一份自己改就好了

只要修改命令参数就OK。

 这样修改

4  打开命令行工具:

 cd 到tolua文件夹  跑脚本 testgenbindings.py  编译就完成了。

这里可能会报错,

LibclangError: dlopen(libclang.dylib, 6): image not found. To provide a path to libclang use Config.set_library_path() or Config.set_library_file().

错误信息提示 找不到libclang 需要调用一下 Config.set_library_path() 或者 Config.set_library_file() 方法


这个问题是因为生成Lua绑定的时候需要 libclang 这个库
查看目录 YOUR_PROJECT/frameworks/cocos2d-x/tools/bindings-generator/libclang 会发现Cocos引擎中提供了这个库
所以,只需要设置一下libchang路径就可以了。

修改 frameworks/cocos2d-x/tools/bindings-generator/generator.py 

974行左右

class Generator(object):
    def __init__(self, opts):
        -- 加入下面两行
        libchangPath = os.path.abspath(os.path.join(os.path.dirname(__file__), 'libclang'))
        cindex.Config.set_library_path(libchangPath)

        self.index = cindex.Index.create()
        self.outdir = opts['outdir']
        self.prefix = opts['prefix']

编译成功就得到了两个文件

lua_mytestbind_auto.hpp

lua_mytestbind_auto.cpp

导入到工程,位置第一个截图可以看到,同MyTestBind 一个目录

5 最后在工程运行刚刚的代码

在lua_module_register.cpp 文件中加入头文件

#include "lua_mytestbind_auto.hpp"  

这个文件的  lua_module_register 函数中加入

register_all_mytestbind(L);  -- 名字是lua_mytestbind_auto.hpp 中定义的

最后lua 中调用

print(MyTestBind:getInstance():fooTest(3))
或
        local tt = MyTestBind:create()
        print(tt:doTestLog())

猜你喜欢

转载自blog.csdn.net/hlmfjkqaz/article/details/82114080
今日推荐