tolua

先举例类名为:SNSprite

--.h文件

#ifndef __LuaDemo__SNSprite__

#define __LuaDemo__SNSprite__

#include “cocos2d.h”

USING_NS_CC;

class SNSprite : public CCSprite{

public:

static SNSprite* create(const char* name);

private:

void initData();

};

#endif /* defined(__LuaDemo__SNSprite__) */

--cpp文件

#include "SNSprite.h"

SNSprite* SNSprite::create(const char* name){

SNSprite* sprite = new SNSprite();

if(sprite && sprite->initWithFile(name)){

sprite->initData();

sprite->autorelease();

return sprite;

}

CC_SAFE_DELETE(sprite);

return NULL;

}

void SNSprite::initData(){

CCActionInterval* action =

CCSequence::createWithTwoActions(CCMoveTo::create(1.0, ccp(300,300)), CCMoveTo::create(1.0, ccp(100,100))

);

this->runAction(CCRepeatForever::create(action));

}

步骤1:

创建一个自己的类或者是cocos2dx自带的类

步骤2:

在cocos2dx\cocos2d-2.1rc0-x-2.1.3\tools\tolua++路径下创建SNSprite.pkg文件

步骤3:

编写SNSprite.pkg文件的内容如下:

class SNSprite : public CCSprit

{

static SNSprite* create(const char* name);

};

步骤4:

打开Cocos2dx.pkg文件,在最后一行加入我们的注册声明:

$pfile “SNSprite.pkg”

步骤5:

打开cmd 运行tolua++.exe执行build.sh脚本

具体操作如下:

1.x: 表示进入哪个磁盘

2. cd +目录

3. tolua++ build.sh

执行后,在你定义的输出目录就生成了新的LuaCocos2d.c[[文件,我们自己定义的类也添加进去了。在Lua中调用:

local function createSunnyLayer()

local layerSunny = CCLayer:create()

local sp = SNSprite:create("HelloWorld.png")

sp:setPosition(ccp(100,100))

layerSunny:addChild(sp)

return layerSunny

end

需要注意的地方:

SNSprite文件最好和LuaCocos2d文件放一起,且在LuaCocos2d.cpp上加上

相关文档
最新文档