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、

2、


之后在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)

执行效果:



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)




本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4908314.html,如需转载请自行联系原作者


相关文章
|
8月前
|
C++
C++ 底层语言的信仰-指针分类
C++ 底层语言的信仰-指针分类
|
20天前
|
存储 Java C++
leetcode 2525 根据规则将箱子分类 c++ pyhton java c题解
leetcode 2525 根据规则将箱子分类 c++ pyhton java c题解
14 1
|
1月前
|
存储 算法 C++
第 284 场周赛(C++ | 枚举 | 分类讨论 | 最短路 | 建反图)
【4月更文挑战第1天】- [LeetCode 6031](https://leetcode-cn.com/problems/find-all-k-distant-indices-in-an-array/):给定数组 `nums`、键值 `key` 和距离 `k`,找到所有与键值相等且与任意下标距离不超过 `k` 的下标,返回升序排序的列表。找到最小权重。
29 0
|
3月前
|
算法 测试技术 C++
【数论】【分类讨论】【C++算法】1611使整数变为 0 的最少操作次数
【数论】【分类讨论】【C++算法】1611使整数变为 0 的最少操作次数
|
4月前
|
Python C++ 算法
C/C++每日一练(20230406) 按要求求质数、两数之和、颜色分类
C/C++每日一练(20230406) 按要求求质数、两数之和、颜色分类
30 0
C/C++每日一练(20230406) 按要求求质数、两数之和、颜色分类
|
机器学习/深度学习 C++ 异构计算
简单线性回归和分类讲解及python和c++实现
入门机器学习,首先要接触到的就是线性回归,里面包含的思想是后面机器学习的一个重要的基础。(ps:趁着简单,多琢磨琢磨)
166 0
简单线性回归和分类讲解及python和c++实现
|
11月前
|
Ubuntu 云计算 C++
C++与lua的结合,LuaBridge的使用及遇到的坑
C++与lua的结合,LuaBridge的使用及遇到的坑
|
11月前
|
编译器 C++
C++ 构造函数的分类及调用
C++ 构造函数的分类及调用
76 0
|
12月前
|
C++
[✔️]lua中的function,在c++进行callback
[✔️]lua中的function,在c++进行callback
137 0
|
12月前
|
C++
[✔️]c++返回2个值给lua,tolua如何处理c++函数重载
[✔️]c++返回2个值给lua,tolua如何处理c++函数重载
90 0