步骤一:首先自定义类(这里Himi自定义类名 “MySprite”)

MySprite.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//
// 
MySprite.h
// 
mtet
//
// 
Created by Himi on 13-4-7.
//
//
 
#ifndef
__mtet__MySprite__
#define
__mtet__MySprite__
 
#include
"cocos2d.h"
usingnamespacecocos2d;
 
classMySprite
:
publicCCSprite{
public:
    staticMySprite*
createMS(
constchar*
fileName);
};
#endif
/* defined(__mtet__MySprite__) */

MySprite.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//
// 
MySprite.cpp
// 
mtet
//
// 
Created by Himi on 13-4-7.
//
//
 
#include
"MySprite.h"
MySprite*
MySprite::createMS(
constchar*
fileName){
    MySprite*
sp =
newMySprite();
    if(sp
&& sp->initWithFile(fileName)){
        sp->setPosition(ccp(100,100));
        sp->autorelease();
        returnsp;
    }
    CC_SAFE_DELETE(sp);
    returnNULL;
}

步骤二:利用tolua++编译我们创建的pkg,将自定义类嵌入LuaCocos2d.cpp中

首先我们到cocos2dx引擎目录下找到tools下的tolua++文件夹。

然后你看到很多的pkg文件,你可以使用文本打开,就会发现都是Cocos2dx引擎封装的类、函数定义,如下CCSprite.pkg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
typedef
enum {
    //!
Translate with it's parent
    CC_HONOR_PARENT_TRANSFORM_TRANSLATE
=  1 << 0,
    //!
Rotate with it's parent
    CC_HONOR_PARENT_TRANSFORM_ROTATE   
=  1 << 1,
    //!
Scale with it's parent
    CC_HONOR_PARENT_TRANSFORM_SCALE    
=  1 << 2,
    //!
Skew with it's parent
    CC_HONOR_PARENT_TRANSFORM_SKEW     
=  1 << 3,
 
    //!
All possible transformation enabled. Default value.
    CC_HONOR_PARENT_TRANSFORM_ALL      
=  CC_HONOR_PARENT_TRANSFORM_TRANSLATE | CC_HONOR_PARENT_TRANSFORM_ROTATE | CC_HONOR_PARENT_TRANSFORM_SCALE | CC_HONOR_PARENT_TRANSFORM_SKEW,
 
}
ccHonorParentTransform;
*/
classCCSprite
:
publicCCNode
{
    voidsetDirty(boolbDirty);
    boolisDirty(void);
 
    ccV3F_C4B_T2F_Quad
getQuad(
void);
 
    CCRect
getTextureRect(
void);
    //bool
isUsesBatchNode(void);
    boolisTextureRectRotated(void);
 
    voidsetAtlasIndex(unsignedintuAtlasIndex);
    unsignedintgetAtlasIndex(void);
    //void
setUsesSpriteBatchNode(bool bUsesSpriteBatchNode);
    voidsetTextureAtlas(CCTextureAtlas
*pobTextureAtlas);
    CCTextureAtlas*
getTextureAtlas(
void);
    //void
setSpriteBatchNode(CCSpriteBatchNode *pobSpriteBatchNode);
    //CCSpriteBatchNode*
getSpriteBatchNode(void);
    //void
setHonorParentTransform(ccHonorParentTransform eHonorParentTransform);
    //ccHonorParentTransform
getHonorParentTransform(void);
    voidsetBlendFunc(ccBlendFunc
blendFunc);
    ccBlendFunc
getBlendFunc(
void);
 
    CCPoint
getOffsetPosition(
void);
 
    voidignoreAnchorPointForPosition(boolnewValue);
    voidsetFlipX(boolbFlipX);
    voidsetFlipY(boolbFlipY);
    boolisFlipX(void);
    boolisFlipY(void);
 
    voidremoveChild(CCNode*
pChild,
boolbCleanUp);
    voidremoveAllChildrenWithCleanup(boolbCleanup);
    voidreorderChild(CCNode*
pChild,
intzOrder);
    voidaddChild(CCNode*
pChild);
    voidaddChild(CCNode*
pChild,
intzOrder);
    voidaddChild(CCNode*
pChild,
intzOrder,inttag);
    voidsortAllChildren();
    //void
setPosition(CCPoint pos);
    voidsetRotation(floatrotation);
    voidsetSkewX(floatsx);
    voidsetSkewY(floatsy);
    voidsetScale(floatfScale);
    voidsetScaleX(floatfScaleX);
    voidsetScaleY(floatfScaleY);
    voidsetVertexZ(floatfVertexZ);
    voidsetAnchorPoint(constCCPoint
& anchor);
    voidsetVisible(boolbVisible);
 
    voidsetOpacity(GLubyte
opacity);
    GLubyte
getOpacity(
void);
 
    voidsetColor(ccColor3B
color3);
    ccColor3B
getColor(
void);
    voidsetOpacityModifyRGB(boolbValue);
    boolisOpacityModifyRGB(void);
 
    voidsetTexture(CCTexture2D
*texture);
    CCTexture2D*
getTexture(
void);
 
    voidupdateTransform(void);
    //void
useSelfRender(void);
    voidsetTextureRect(CCRect
rect);
    voidsetTextureRect(CCRect
rect,
boolrotated,
CCSize size);
    voidsetVertexRect(CCRect
rect);
    //void
useBatchNode(CCSpriteBatchNode *batchNode);
    voidsetDisplayFrame(CCSpriteFrame
*pNewFrame);
    boolisFrameDisplayed(CCSpriteFrame
*pFrame);
    CCSpriteFrame*
displayFrame(
void);
    voidsetBatchNode(CCSpriteBatchNode*
pBatchNode);
    CCSpriteBatchNode*
getBatchNode();
    voidsetDisplayFrameWithAnimationName(constchar*animationName,
intframeIndex);
 
    staticCCSprite*
createWithTexture(CCTexture2D *pTexture);
    staticCCSprite*
createWithTexture(CCTexture2D *pTexture, CCRect rect);
    staticCCSprite*
createWithSpriteFrame(CCSpriteFrame *pSpriteFrame);
    staticCCSprite*
createWithSpriteFrameName(
constchar*pszSpriteFrameName);
    staticCCSprite*
create(
constchar*pszFileName,
CCRect rect);
    staticCCSprite*
create(
constchar*pszFileName);
    staticCCSprite*
create();
};

没错,我们也会按照类似方式进行创建我们自定义类的pkg文件。

我们自定义一个文件(文本、xcode等都可以),后缀 .pkg ,然后将Himi自定义的MySprite类定义到pkg中,如下:

注意:只要自定义类.h中的内容,至于cpp的实现,binding后lua自动调用你类的函数

MySprite.pkg

1
2
3
classMySprite
:
publicCCSprite{
    staticMySprite*
createMS(
constchar*
fileName);
};

在pkg中我只是定义了创建函数而已,至于更多的函数就交给大家自定义啦,另外我们注意书写pkg时是需要几条规则的,其实在tolua++这个文件夹中我们也能看到有一个名字叫 README 的文件,打开如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1.Generatingthelua<-->C
bindings with tolua++
 
    Build
scripts
forwindows(build.bat)andunix
(build.sh)are
provided
    togeneratetherelevant
files
aftermodifyingthe.pkg
files.  These
    scripts
basically
runthefollowing
command
:
 
        tolua++.exe-L
basic.lua
-o
LuaCocos
2d.cpp
Cocos
2d.pkg
 
    This
will generate
thebindingsfileandpatch
itwithcome
cocos
2dx
    specific
modifications.
 
    On
POSIX systems you can also just
run"make"to

build
thebindings
    if/when
you change .pkg files.
 
2.Writing
.pkg files
 
    1)enum
keeps
thesame
    2)removeCC_DLL
fortheclass

defines
,pay
attention
tomulti
inherites
    3)removeinline
keyword
fordeclarationandimplementation
    4)removepublic
protect
andprivate
    5)removethe

decalration
of
class

member variable
    6)keep
static keyword
    7)removememeber
functions
thatdeclaredasprivateorprotected

这个文件声明了书写pkg的规则,不多赘述。

书写好我们的pkg之后,将pkg文件放置此tolua++文件夹下即可,然后配置我们tolua++工具。

继续在tolua++文件夹中解压tolua++.Mac.zip 文件,会得到一个tolua++的工具,如下图:

解压出工具之后,我们还要在tolua++文件夹中,配置tolua++路径,打开“build.sh”文件,如下:

这里 TOLUA 是tolua++工具的位置(路径后面要架上 /tolua++  表示这个工具),最下面配置的是编译后的luaCocos2d.cpp文件导出的位置,Himi这里配置到桌面,配置如下:

最后,我们要将我们定义的pkg文件注册到 tolua++文件夹下的Cocos2d.pkg中,如下:

如上步骤都OK后,我们就可以使用“终端”,先cd到tolua++的文件夹下,然后使用“make”命令执行tolua++工具 或者终端输入 ./build.sh 。

(如果这里终端不能正常执行, 请继续修改tolua++文件夹下的: makefile  ,将其路径配置一下即可。)

终端正常执行后,会在一开始指定的目录生成LuaCocos2d.cpp 文件,且其中已经binding好了自定义类,将生成的LuaCocos2d.cpp替换到你项目的/libs/lua/cocos2dx_support下的LuaCocos2d.cpp 文件。

Himi建议生成的LuaCocos2d.cpp 文件路径直接设置你的项目的/libs/lua/cocos2dx_support下很方便

注意:这时候LuaCoco2d.cpp中虽然已经binding了我们的自定义类,但是没有引用我们的头文件,所以我们还需要在LuaCocos2d.h中倒入我们自定义类.h 。

步骤三:Lua测试我们的自定义类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
--
for CCLuaEngine traceback
function
__G__TRACKBACK__
(msg)
    print("----------------------------------------")
    print("LUA
ERROR: "

.. tostring
(msg).."\n")
    print(debug.traceback())
    print("----------------------------------------")
end
 
localfunctionmain()
    --
avoid memory leak
    collectgarbage("setpause",100)
    collectgarbage("setstepmul",5000)
 
    localcclog=function(...)
        print(string.format(...))
    end
 
    require"hello2"
    cclog("result
is "

.. myadd
(3,5))
 
    ---------------
 
    --
create farm
    localfunction
createLayerFarm
()
        locallayerFarm=CCLayer:create()
 
        localfont=

CCLabelTTF
:create("Himi
使用tolua++ binding自定义类"
,"Verdana-BoldItalic",20)
        font:setPosition(ccp(220,260))
        layerFarm:addChild(font)
 
        localms =MySprite:createMS("Icon.png")
        layerFarm:addChild(ms)
 
        returnlayerFarm
    end
 
    --
run
    localsceneGame=CCScene:create()
    sceneGame:addChild(createLayerFarm())
    CCDirector:sharedDirector():runWithScene(sceneGame)
end
 
xpcall(main,__G__TRACKBACK__)

运行截图如下:

原文链接: http://www.himigame.com/lua-game/1259.html

使用tolua++编译pkg,从而创建自定义类让Lua脚本使用的更多相关文章

  1. cocos2d-x-lua如何导出自定义类到lua脚本环境

      这篇教程是基于你的工程是cocos2d-x-lua的项目,我假设你已经完全驾驭cocos-x/samples/Lua/HelloLua工程,基本明白lua和c++互调的一些原理. 我们的目的是要在 ...

  2. 【COCOS2DX-LUA 脚本开发之四】使用tolua++编译pk创建自定义类

    此篇基本[COCOS2DX(2.X)_LUA开发之三]在LUA中使用自定义精灵(LUA脚本与自创建类之间的访问)及LUA基础讲解 在Lua第三篇中介绍了,如何在cocos2dx中使用Lua创建自定义类 ...

  3. cocos2dx 2.x版本:简化提炼tolua++绑定自定义类到lua中使用

    cocos2dx的3.x版本已经提供了更好地绑定方式,网上有很多相关的教程,这里给一个链接:http://www.cocoachina.com/bbs/read.php?tid=196416. 由于目 ...

  4. cocos2dx-3.x 导出自定义类到 lua 过程详解

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.简介 最近正在学习cocos2d中的lua游戏开发,因为lua开发的热更新特性,大家开发游戏好像都会优先选择lua作为 ...

  5. cocos2dx3.0rc导出自定义类到lua的方法

    以前要导出c++类到lua,就得手动维护pkg文件,那简直就是噩梦,3.0以后就会感觉生活很轻松了. 转载请注明出处http://www.cnblogs.com/mrblue/p/3637910.ht ...

  6. cocos2dx3.0导出自定义类到lua的方法详细步骤

    我写了一个用3.0的工具导出类到lua,自动生成代码的方法. 以前要导出c++类到lua,就得手动维护pkg文件,那简直就是噩梦,3.0以后就会感觉生活很轻松了. 下面我就在说下具体做法.1.安装必要 ...

  7. win7系统cocos2dx 3.4 绑定自定义类到Lua

    Cocos2d-x 3.0开始使用bindings-generator来生成c++类的lua绑定.bindings-generator基于tolua++,通过配置tools/tolua中的ini文件以 ...

  8. java创建自定义类的数组

    今天在学图论的最小生成树,开始一直在想是用邻接矩阵还是关联矩阵来表示图,但是发现这样都会有好多空间浪费.于是我就自定义一个边的类,里面包含了权值,关联的端点1,端点2,和图的表示字母.发现我想创建11 ...

  9. java创建自定义类的对象数组

      1 public class Student{ 2 static int number = 0; // 静态变量的访问可以不用创建类的实例就可就可使用< 类名.属性 >的方法访问 3 ...

随机推荐

  1. [转]网站优化-IIS7下静态文件的优化

    本文转自:http://www.cnblogs.com/Leung/archive/2009/10/26/1590256.html 在网站开发过程中,通常我们会对网站的静态文件做处事,像图片文件,CS ...

  2. MultiMap、BidiMap及LazyMap的使用

    一.MultiMap 在日常的开发工作中,我们有的时候需要构造像Map<K, List<V>>或者Map<K, Set<V>>这样比较复杂的集合类型的数 ...

  3. display:none与visible:hidden区别

    if(list.style.display=='none'){                 list.style.display='block';             }else{       ...

  4. Ajax中解析Json的两种方法详解

    eval();  //此方法不推荐 JSON.parse();  //推荐方法 一.两种方法的区别 我们先初始化一个json格式的对象: var jsonDate = '{ "name&qu ...

  5. java演示facade(外观)模式

    实际应用中,原来的代码涉及多个子系统时,重新进行类的设计,将原来分散在源码中的类结构及方法重新组合,形成新的.统一的接口,供上层应用使用. Facade所面对的往往是多个类或其它程序单元,通过重新组合 ...

  6. Lombok 安装

    Lombok 是一种 Java™ 实用工具,可用来帮助开发人员消除 Java 的冗长,尤其是对于简单的 Java 对象(POJO).它通过注释实现这一目的.通过在开发环境中实现 Lombok ,开发人 ...

  7. Linux C编程--格式化I/O

    printf(格式控制,输入表列) 例:printf("%d%d",a,b) (1)d格式符:输出一个有符号的十进制整数 (2)c格式符:输出一个字符 (3)s格式符:输出一个字符 ...

  8. C标准库函数实现之strstr(转)

    看下Linux下的实现: char *strstr(const char *s1, const char *s2) { size_t l1, l2; l2 = strlen(s2); if (!l2) ...

  9. JAVA.IO流学习笔记

    一.java.io 的描述 通过数据流.序列化和文件系统提供系统输入和输出.IO流用来处理设备之间的数据传输 二.流 流是一个很形象的概念,当程序需要读取数据的时候,就会开启一个通向数据源的流,这个数 ...

  10. 工具:linux 性能监控工具-nmon

    nmon 下载:http://pan.baidu.com/s/1jICoSpo 放到linux下可以直接运行. nmon 工具可以帮助在一个屏幕上显示所有重要的性能优化信息,并动态地对其进行更新. n ...