cocos2d-lua 学习笔记—1

来源:互联网 发布:mac repo 下载失败 编辑:程序博客网 时间:2024/05/18 03:01
cocos2d-lua 学习笔记—1


1、cocos2d-lua版本分类:普通版/Quick版,现在已经合并


2、工程的创建
a、Cocos Code IDE:配置环境后直接创建,如果需要创建xcode可以运行的工程,需要在Cocos Tools -> Add Native Code support...


b、使用命令行 cocos new -p com.lsw.game -l lua -d . HelloWorld创建


3、lua的绑定
具体的原理可以参考网址:
(Cocos2d-x下Lua调用自定义C++类和函数的最佳实践)
http://segmentfault.com/a/1190000000631630


具体的过程可以参考:
(我所理解cocos2d-x 3.6 lua -- Cocos如何绑定Lua自定义类)
http://blog.csdn.net/rexuefengye/article/details/46553239


这两篇文章结合后可以完成绑定,这里在简单的总结一下自己的绑定过程,以及其中需要注意的事项。
(1)、新建自己的类,比如在文件夹下frameworks/runtime-src/Classes/建立新的类


MyTestLua.h:


#include "cocos2d.h"
class MyTestLua : public cocos2d::Ref {
public:
    MyTestLua(){};
    ~MyTestLua(){};
    bool init() {
        return true;
    };
    int func(int i);
    CREATE_FUNC(MyTestLua);
};


MyTestLua.cpp:


#include "MyTestLua.h"
int MyTestLua::func(int i) {
    return i + 10;
}


(2)、跳转到工程目录下的文件夹 frameworks/cocos2d-x/tools/tolua,可以看到目录下有很多init的文件,这些就是我们需要绑定的所有的类的配置文件。还有一个genbindings.py的文件,这个python文件就是绑定需要的python文件。同时还需要注意的是运行这个python文件需要pyyaml、Cheetah这两个包,安装方法网上有很多。


(3)、拷贝一个init文件,然后修改名字为我们需要的名字,比如MyTestLua.init


(4)、修改MyTestLua.init文件的内容,主要是修改以下几个地方,init文件的内容参数具体的意义后面会介绍。
[MyTestLua]  
prefix           = MyTestLua 
target_namespace = lsw                   
headers          = %(cocosdir)s/../runtime-src/Classes/MyTestLua.h
classes          = MyTestLua 


完整的init如下:
[MyTestLua]
# the prefix to be added to the generated functions. You might or might not use this in your own
# templates
prefix = MyTestLua


# 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 = lsw


# the native namespace in which this module locates, this parameter is used for avoid conflict of the same class name in different modules, as "cocos2d::Label" <-> "cocos2d::ui::Label".
cpp_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 -U __SSE__


cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/editor-support -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/../runtime-src/Classes/MyTestLua.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 = MyTestLua


# 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


(5)、修改genbindings.py文件
cmd_args = {'cocos2dx.ini' : ('cocos2d-x', 'lua_cocos2dx_auto'), \  
            'MyTestLua.ini' : ('MyTestLua', 'lua_MyTestLua_auto'), \  
            ...  


(6)、运行python genbindings.py,运行的过程中可能会有warning,可以忽略它们


(7)、等待片刻后,查看cocos2d_lua_bindings.xcodeproj,在auto文件的目录中可以看到自动生成的
lua_MyTestLua_auto.cpp和lua_MyTestLua_auto.hpp,将这两个文件添加到工程中


(8)、在cpp文件中由于引用了MyTestLua.h这个文件,但是这个工程的头文件中并没有查找这个的路径。需要修改一下User Header Search Paths,添加路径$(SRCROOT)/../../../../../runtime-src/Classes


(9)、添加测试例子
function MainScene:myAdd()
    local t = lsw.MyTestLua:create()
    print("lsw myTestLua value is "..t:func(100))
end


在onCreate方法中调用 self:myAdd()


(10)、运行工程
可以看到输出  lsw myTestLua value is 110


这里只是实际操作lua绑定,具体的原理和解释还需要在仔细的研究。

















0 0
原创粉丝点击