微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用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"
using namespace cocos2d;
 
class MySprite : public CCSprite{
:
     static MySprite* createMS( const char * fileName);
};
#endif /* defined(__mtet__MySprite__) */

 

MySprite.cpp

//  MySprite.cpp
#include "MySprite.h"
MySprite* MySprite::createMS( * fileName){
     MySprite* sp = new MySprite();
if (sp && sp->initWithFile(fileName)){
         sp->setPosition(ccp(100,100));
sp->autorelease();
         return sp;
}
CC_SAFE_DELETE(sp);
NULL;
}

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

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

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

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;
*/
CCSprite : CCNode
{
void setDirty( bool bDirty);
     isDirty( );
 
ccV3F_C4B_T2F_Quad getQuad( );
 
CCRect getTextureRect( );
//bool isUsesBatchNode(void);
isTextureRectRotated( );
 
setAtlasIndex(unsigned int uAtlasIndex);
unsigned getAtlasIndex( );
//void setUsesspriteBatchNode(bool bUsesspriteBatchNode);
setTextureAtlas(CCTextureAtlas *pobTextureAtlas);
CCTextureAtlas* getTextureAtlas( );
//void setSpriteBatchNode(CCSpriteBatchNode *pobSpriteBatchNode);
//CCSpriteBatchNode* getSpriteBatchNode(void);
//void setHonorParentTransform(ccHonorParentTransform eHonorParentTransform);
//ccHonorParentTransform getHonorParentTransform(void);
setBlendFunc(ccBlendFunc blendFunc);
ccBlendFunc getBlendFunc( );
 
CCPoint getoffsetPosition( );
 
ignoreAnchorPointForPosition( newValue);
setFlipX( bFlipX);
setFlipY( bFlipY);
isFlipX( );
isFlipY( );
 
removeChild(CCNode* pChild, bCleanUp);
removeAllChildrenWithCleanup( bCleanup);
reorderChild(CCNode* pChild,monospace!important; font-size:1em!important; line-height:1.1em!important; overflow:visible!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none!important"> zOrder);
addChild(CCNode* pChild);
addChild(CCNode* pChild,monospace!important; font-size:1em!important; line-height:1.1em!important; overflow:visible!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none!important"> zOrder);
zOrder,monospace!important; font-size:1em!important; line-height:1.1em!important; overflow:visible!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none!important"> tag);
sortAllChildren();
//void setPosition(CCPoint pos);
setRotation( float rotation);
setSkewX( sx);
setSkewY( sy);
setScale( fScale);
setScaleX( fScaleX);
setScaleY( fScaleY);
setVertexZ( fVertexZ);
setAnchorPoint( const CCPoint & anchor);
setVisible( bVisible);
 
setopacity(glubyte opacity);
glubyte getopacity( );
 
setColor(ccColor3B color3);
ccColor3B getColor( );
setopacityModifyRGB( bValue);
isOpacityModifyRGB( );
 
setTexture(CCTexture2D *texture);
CCTexture2D* getTexture( );
 
updateTransform( );
//void useSelfRender(void);
setTextureRect(CCRect rect);
setTextureRect(CCRect rect,monospace!important; font-size:1em!important; line-height:1.1em!important; overflow:visible!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none!important"> rotated,CCSize size);
setVertexRect(CCRect rect);
//void useBatchNode(CCSpriteBatchNode *batchNode);
setdisplayFrame(CCSpriteFrame *pNewFrame);
isFramedisplayed(CCSpriteFrame *pFrame);
CCSpriteFrame* displayFrame( );
setBatchNode(CCSpriteBatchNode* pBatchNode);
CCSpriteBatchNode* getBatchNode();
setdisplayFrameWithAnimationName( *animationName,monospace!important; font-size:1em!important; line-height:1.1em!important; overflow:visible!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none!important"> frameIndex);
 
CCSprite* createWithTexture(CCTexture2D *pTexture);
CCSprite* createWithTexture(CCTexture2D *pTexture,CCRect rect);
CCSprite* createWithSpriteFrame(CCSpriteFrame *pSpriteFrame);
CCSprite* createWithSpriteFrameName( *pszSpriteFrameName);
CCSprite* create( *pszFileName,CCRect rect);
*pszFileName);
CCSprite* create();
};

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

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

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

MySprite.pkg

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

23
1. Generating the lua < -->C bindings with tolua++
 
     Build scripts for windows ( build.bat ) and unix build.sh are provided
to generate relevant files after modifying .pkg files.  These
scripts basically run the following command :
 
         tolua + .exe - L basic.lua o LuaCocos 2 d.cpp Cocos d.pkg
 
This will generate bindings file patch it with come cocos dx
specific modifications.
 
On POSIX systems you can also just "make" to build bindings
if / when you change .pkg files.
 
2. Writing .pkg files
 
1 enum keeps same
remove CC_DLL class defines , pay attention multi inherites
3 inline keyword declaration and implementation
4 public protect private
5 decalration of class member variable
6 keep static keyword
7 memeber functions that declared as private or protected

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

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

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

QQ20130407-2

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

1

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

QQ20130407-4

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

QQ20130407-7

如上步骤都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测试我们的自定义

43
-- for ccluaEngine traceback
function __G__TRACKBACK__ msg )
print "----------------------------------------" )
"LUA ERROR: " .. tostring .. "\n" )
debug.traceback )
)
end
 
local function main )
-- avoid memory leak
collectgarbage "setpause" 100 )
"setstepmul" 5000 )
 
cclog = function ... )
string .format )
end
 
require "hello2"
cclog "result is " .. myadd 5 )
 
---------------
 
-- create farm
function createLayerFarm )
layerFarm cclayer : create )
 
font = cclabelTTF "Himi 使用tolua++ binding自定义类" "Verdana-BoldItalic" 20 )
font setPosition ccp 220 260 )
layerFarm addChild )
 
ms  MySprite createMS "Icon.png" )
ms )
 
return layerFarm
end
 
-- run
sceneGame CCScene )
sceneGame createLayerFarm )
CCDirector sharedDirector runWithScene )
end
 
xpcall __G__TRACKBACK__ )

运行截图如下:

QQ20130407-6

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐