这里记录下我实现Lua绑定的全过程。
准备工作:
1、创一个一个Lua的2dx工程。(这个网上已经有好多了)
2、创一个C++类。
TestScene.h 这个只是一个简单的场景
//
// TestScene.h
// uitestLua
//
// Created by 杜甲 on 14-5-17.
//
//
#ifndef __uitestLua__TestScene__
#define __uitestLua__TestScene__
#include cocos2d.h
USING_NS_CC;
class TestScene :public Layer{
public:
static Scene* createScene();
virtual bool init();
CREATE_FUNC(TestScene);
};
#endif /* defined(__uitestLua__TestScene__) */
TestScene.cpp
//
// TestScene.cpp
// uitestLua
//
// Created by 杜甲 on 14-5-17.
//
//
#include TestScene.h
Scene* TestScene::createScene()
{
auto layer= TestScene::create();
auto scene = Scene::create();
scene->addChild(layer);
return scene;
}
bool TestScene::init()
{
bool bRet = false;
do {
CC_BREAK_IF(!Layer::init());
auto sprite = Sprite::create(res/dog.png);
sprite->setPosition(Point(100, 200));
addChild(sprite);
bRet = true;
} while (0);
return bRet;
}
上面的代码很简单没什么好说的。接下是本文的主要内容。将上面的C++类绑定Lua
步骤:
1、绑定基本变量:这个按照readme中的去做,注意NDK要用:r9b一定要用这个。
2、编译自己的ini文件。这个需要参照其他的ini。我参考的是cocos2dx_physics.ini
自己创建一个文本文件将cocos2dx_physics.ini中的内容全部粘过来,之后我们只需要修改几个地方。
下面是修改好的脚本:
testscene.ini
[testscene]
# the prefix to be added to the generated functions. You might or might not use this in your own
# templates
prefix = testscene
# create a target namespace (in java script, 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
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/cocos/2d -I%(cocosdir)s/cocos/base -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/cocos/physics
cocos_flags = -DANDROID -DCOCOS2D_java script -DCC_USE_PHYSICS=1
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 = /Users/tokou/WORK/5-Cocos2dx/Project/UI/uitestLua/frameworks/runtime-src/Classes/TestScene.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 = TestScene
# 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
re