使用tolua++编译pkg,从而创建自定义类让Lua脚本使用
步骤一:首先自定义类(这里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
LuaCocos2d.cpp
Cocos2d.pkg This
will generate thebindingsfileandpatch
itwithcome
cocos2dx 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 tracebackfunction
__G__TRACKBACK__(msg) print("----------------------------------------") print("LUA
ERROR: "
.. tostring(msg).."\n") print(debug.traceback()) print("----------------------------------------")endlocalfunctionmain() --
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)endxpcall(main,__G__TRACKBACK__) |
运行截图如下:
原文链接: http://www.himigame.com/lua-game/1259.html






浙公网安备 33010602011771号