首先进入准备工作
1.安装软件
打开终端,逐条执行下面的命令
sudo easy_install pipsudo pip install PyYAML
sudo pip install Cheetah
*****************************
pip是一个安装和管理Python包的工具
PyYAML是一个Python的YAML解析器,这是一种数据序列化语言,是一种可读的文本的数据结构,它的设计目标是使人们容易读,程序容易处理。它类似XML,但是比XML简单。
Cheetah提供了一门简单语言,用来定义提供基本流控制和对象访问构造的模板。
2.增加NDK_ROOT,必须是android-ndk-r9b及以上
二:编写自己的c++代码
#ifndef _HELLO_H_
#define _HELLO_H_
#include "cocos2d.h"
namespace cocos2d {
class Hello : public cocos2d::Ref{
public:
Hello(){};
~Hello(){};
bool init(){return true;};
static cocos2d::Hello* create()
{
Hello *h = new Hello();
if(h && h->init()){
h->autorelease();
return h;
}
return NULL;
};
void myprint(){log("Hello Hello");};
};
}
#endif
三:编写cocos2dx_hello.ini
1.在项目的frameworks/cocos2d-x/tools/tolua/下创建一个文件cocos2dx_hello.ini。
2.将以下内容:
[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 = cc 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/my -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/cocos/my/CustomClass.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 = CustomClass.* # 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拷贝到cocos2dx_hello.ini中。 3.修改cocos2dx_hello.ini文件。
A.第1行的名字 [cocos2dx_hello]
B.第4行注释掉 #not use this in your own
C.第8行的前缀 prefix = cocos2dx_hello
D.第15行的命名空间 target_namespace = cc
E.第37行的头文件 headers = %(cocosdir)s/cocos/my/Hello.h
F.第43行的类名 classes = Hello.*
4.修改genbindings.py文件
修改136行的 cmd_args在最后加上
'cocos2dx_hello.ini' : ('cocos2dx_hello', 'lua_cocos2dx_hello_auto'), \
5.打开终端,进入到frameworks/cocos2d-x/tools/tolua/目录,输入export PYTHON_BIN=/usr/bin/python
6.执行genbindings.py
在项目的framework/cocos2d-x/cocos/scripting/lua-bindings/auto/目录中会有lua_cocos2dx_hello_auto.cpp和lua_cocos2dx_hello_auto.hpp文件。
四:在lua中使用
1.使用Xcode6.1打开项目
2.lua_cocos2dx_hello_auto.cpp导入到项目
3.build settings的User Header Search paths debug中增加$(SRCROOT)/../../cocos2d-x/cocos/my
4.在lua_module_register.h中导入#include “lua_cocos2dx_hello_auto.hpp"头文件
5.lua_module_register.h中int lua_module_register(lua_State* L)方法中增加
lua_getglobal(L,"_G");
if(lua_istable(L,-1))
{
register_all_cocos2dx_hello(L);
}
lua_pop(L,1);
6.在MainScene.lua中编写local h = cc.Hello:create();
h:myprint();
7.运行项目,可以看到“Hello Hello”