Cocos2d-x3.0 lua捆绑C++分类

我知道这个纪录Lua结合整个过程。

原文地址:http://blog.csdn.net/qqmcy/article/details/26099859

准备工作:

1、创一个一个Lua的2dxproject。(这个网上已经有好多了)

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;
}

上面的代码非常easy没什么好说的。

接下是本文的主要内容。将上面的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 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
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_JAVASCRIPT -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?
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 = Layer # 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

主要改动的地方:

[testscene]
prefix = testscene
target_namespace =
headers = /Users/tokou/WORK/5-Cocos2dx/Project/UI/uitestLua/frameworks/runtime-src/Classes/TestScene.h --这里我用的是绝对路径用,假设用之前的变量路径找不到 TestScene.h
classes = TestScene
skip =
abstract_classes =

之后我们再自己写一个genbindings_testscene.py脚本这个脚本就是在genbindings.py这个脚本的基础上改的。

仅仅要改动命令參数就OK。

将:

 cmd_args = {'cocos2dx.ini' : ('cocos2d-x', 'lua_cocos2dx_auto'), \
'cocos2dx_extension.ini' : ('cocos2dx_extension', 'lua_cocos2dx_extension_auto'), \
'cocos2dx_ui.ini' : ('cocos2dx_ui', 'lua_cocos2dx_ui_auto'), \
'cocos2dx_studio.ini' : ('cocos2dx_studio', 'lua_cocos2dx_studio_auto'), \
'cocos2dx_spine.ini' : ('cocos2dx_spine', 'lua_cocos2dx_spine_auto'), \
'cocos2dx_physics.ini' : ('cocos2dx_physics', 'lua_cocos2dx_physics_auto'), \ }

改动成:

cmd_args = {'testscene.ini': ('testscene','lua_testscene_auto') }

之后打开命令行工具:

cd 到tolua目录

./genbindings_testscene.py

编译就完毕了。

我们接下来使用下面我们自己的类。

首先,导入我们编译出来的.hpp和.cpp文件。他们在哪里呢?

见下图:

1、

Cocos2d-x3.0  lua捆绑C++分类

2、Cocos2d-x3.0  lua捆绑C++分类

之后在AppDelegate.cpp

增加头文件:

#include "lua_testscene_auto.hpp"

在 bool AppDelegate::applicationDidFinishLaunching()中增加例如以下代码。

auto engine = LuaEngine::getInstance();
ScriptEngineManager::getInstance()->setScriptEngine(engine);
engine->executeScriptFile("src/test2.lua");
/***************这里增加例如以下代码*/
register_all_testscene(engine->getLuaStack()->getLuaState());

.lua

	scene = TestScene:createScene()
cc.Director:getInstance():runWithScene(scene)

执行效果:

Cocos2d-x3.0  lua捆绑C++分类

2014.7.1

补充:

test2.lua

function createLayer( )
local layer1 = cc.Layer:create() local label1 = cc.Label:createWithBMFont("res/fonts/bitmapFontTest2.fnt", "Test") label1:setAnchorPoint( cc.p(0,0) )
layer1:addChild(label1,0,1) local fade = cc.FadeOut:create(1.0)
local fade_in = fade:reverse() local seq = cc.Sequence:create(fade,fade_in)
local repeatAction = cc.RepeatForever:create(seq)
label1:runAction(repeatAction) local spriteNormal = cc.Sprite:create("res/menu2.png")
local spriteSelected = cc.Sprite:create("res/menu1.png")
local spriteDisabled = cc.Sprite:create("res/menu1.png") local item1 = cc.MenuItemSprite:create(spriteNormal, spriteSelected, spriteDisabled) local function menuCallback(sender)
print("menuCallback...") require "src/helloworld"
scene2 = createScene()
cc.Director:getInstance():replaceScene(scene2) end
item1:registerScriptTapHandler(menuCallback) local menu = cc.Menu:create()
menu:addChild(item1)
menu:setPosition(cc.p(100,200))
layer1:addChild(menu)
return layer1
end local scene = cc.Scene:create()
local layer = createLayer( ) scene:addChild(layer)
cc.Director:getInstance():runWithScene(scene)

版权声明:本文博主原创文章,博客,未经同意不得转载。

上一篇:Berkeley DB 使用


下一篇:rpm安装软件时提示warning: *.rpm: Header V3 RSA/SHA256 Signature, keykey ID c105b9de