【木头Cocos2d-x 008】在Lua中使用自定义类——tolua++工具使用(下集)

在Lua中使用自定义类——tolua++工具使用(下集)

 

 

笨木头花心贡献,啥?花心?不呢,是用心~

转载请注明,原文地址
 http://blog.csdn.net/musicvs/article/details/8166655 

 

正文:

上回说到,把LuaCocos2d.cpp文件拷到我们的lua工程里,然后,编译。

 

大功告成……啊才怪啊~!

 

 

你会发现一大堆的编译错误,超过100个了,木了个头的。怎么回事,我只能认定是这个工具出问题了。

怎么办?没关系~我们在LuaCocos2d.cpp里搜索一下我们的SpriteFactory类吧,肯定有的,看看其中一段:

/* method: sharedSpriteFactory of class  SpriteFactory */
#ifndef TOLUA_DISABLE_tolua_Cocos2d_SpriteFactory_sharedSpriteFactory00
static int tolua_Cocos2d_SpriteFactory_sharedSpriteFactory00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
    tolua_Error tolua_err;
    if (
        !tolua_isusertable(tolua_S,1,"SpriteFactory",0,&tolua_err) ||
        !tolua_isnoobj(tolua_S,2,&tolua_err)
        )
        goto tolua_lerror;
    else
#endif
    {
        {
            SpriteFactory* tolua_ret = (SpriteFactory*)  SpriteFactory::sharedSpriteFactory();
            tolua_pushusertype(tolua_S,(void*)tolua_ret,"SpriteFactory");
        }
    }
    return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
    tolua_error(tolua_S,"#ferror in function 'sharedSpriteFactory'.",&tolua_err);
    return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE

知道我想说什么吧?既然它已经能生成自定类的这些代码了,那还怕什么。

现在回到生成LuaCocos2d.cpp的步骤,打开Cocos2d.pkg文件,把其它所有无关的类删除~!变成这样:

$#include "LuaCocos2d.h"

$pfile " SpriteFactory.pkg"


 

然后继续输入命令,生成LuaCocos2d.cpp文件。

于是,这样生成的文件就只有我们自定义类的声明代码了:

#include "LuaCocos2d.h"

/* function to register type */
static void tolua_reg_types (lua_State* tolua_S)
{
#ifndef Mtolua_typeid
#define Mtolua_typeid(L,TI,T)
#endif
 tolua_usertype(tolua_S,"CCSprite");
 Mtolua_typeid(tolua_S,typeid(CCSprite), "CCSprite");
 tolua_usertype(tolua_S,"CCLayer");
 Mtolua_typeid(tolua_S,typeid(CCLayer), "CCLayer");
 tolua_usertype(tolua_S,"SpriteFactory");
 Mtolua_typeid(tolua_S,typeid(SpriteFactory), "SpriteFactory");
}

/* method: sharedSpriteFactory of class  SpriteFactory */
#ifndef TOLUA_DISABLE_tolua_Cocos2d_SpriteFactory_sharedSpriteFactory00
static int tolua_Cocos2d_SpriteFactory_sharedSpriteFactory00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
 tolua_Error tolua_err;
 if (
     !tolua_isusertable(tolua_S,1,"SpriteFactory",0,&tolua_err) ||
     !tolua_isnoobj(tolua_S,2,&tolua_err)
 )
  goto tolua_lerror;
 else
#endif
 {
  {
   SpriteFactory* tolua_ret = (SpriteFactory*)  SpriteFactory::sharedSpriteFactory();
    tolua_pushusertype(tolua_S,(void*)tolua_ret,"SpriteFactory");
  }
 }
 return 1;
#ifndef TOLUA_RELEASE
 tolua_lerror:
 tolua_error(tolua_S,"#ferror in function 'sharedSpriteFactory'.",&tolua_err);
 return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE

/* method: createSprite of class  SpriteFactory */
#ifndef TOLUA_DISABLE_tolua_Cocos2d_SpriteFactory_createSprite00
static int tolua_Cocos2d_SpriteFactory_createSprite00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
 tolua_Error tolua_err;
 if (
     !tolua_isusertype(tolua_S,1,"SpriteFactory",0,&tolua_err) ||
     !tolua_isusertype(tolua_S,2,"CCLayer",0,&tolua_err) ||
     !tolua_isnumber(tolua_S,3,0,&tolua_err) ||
     !tolua_isnoobj(tolua_S,4,&tolua_err)
 )
  goto tolua_lerror;
 else
#endif
 {
  SpriteFactory* self = (SpriteFactory*)  tolua_tousertype(tolua_S,1,0);
  CCLayer* mLayer = ((CCLayer*)  tolua_tousertype(tolua_S,2,0));
  SpriteFactory::SpriteType enSpriteType = ((SpriteFactory::SpriteType) (int)  tolua_tonumber(tolua_S,3,0));
#ifndef TOLUA_RELEASE
  if (!self) tolua_error(tolua_S,"invalid 'self' in function 'createSprite'", NULL);
#endif
  {
   CCSprite* tolua_ret = (CCSprite*)  self->createSprite(mLayer,enSpriteType);
    tolua_pushusertype(tolua_S,(void*)tolua_ret,"CCSprite");
  }
 }
 return 1;
#ifndef TOLUA_RELEASE
 tolua_lerror:
 tolua_error(tolua_S,"#ferror in function 'createSprite'.",&tolua_err);
 return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE

/* Open function */
TOLUA_API int tolua_Cocos2d_open (lua_State* tolua_S)
{
 tolua_open(tolua_S);
 tolua_reg_types(tolua_S);
 tolua_module(tolua_S,NULL,0);
 tolua_beginmodule(tolua_S,NULL);
  tolua_cclass(tolua_S,"SpriteFactory","SpriteFactory","",NULL);
  tolua_beginmodule(tolua_S,"SpriteFactory");
   tolua_constant(tolua_S,"en_close",SpriteFactory::en_close);
   tolua_constant(tolua_S,"en_grossini",SpriteFactory::en_grossini);
   tolua_constant(tolua_S,"en_grossinis_sister",SpriteFactory::en_grossinis_sister);
   tolua_constant(tolua_S,"en_grossinis_sister2",SpriteFactory::en_grossinis_sister2);
   tolua_function(tolua_S,"sharedSpriteFactory",tolua_Cocos2d_SpriteFactory_sharedSpriteFactory00);
   tolua_function(tolua_S,"createSprite",tolua_Cocos2d_SpriteFactory_createSprite00);
  tolua_endmodule(tolua_S);
 tolua_endmodule(tolua_S);
 return 1;
}


#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501
 TOLUA_API int luaopen_Cocos2d (lua_State* tolua_S) {
 return tolua_Cocos2d_open(tolua_S);
};
#endif


 

这就没有问题了,我们不要替换它原有LuaCocos2d.cpp文件,而是依葫芦画瓢地把我们生成的LuaCocos2d.cpp文件里新增的内容拷到原有的文件里。这样编译就基本不会报错了,即使报错,也只是小范围报错,顶多几个,不会出现上百个错误吓死人了。

 

然后编译,运行,没有问题了。

我写了一个lua文件测试我的功能:

local function createLayer()
	local layer = CCLayer:create();
	
	local sprite = SpriteFactory:sharedSpriteFactory():createSprite(layer, SpriteFactory.en_grossini);
	sprite:setPosition(200, 200);
	return layer;
end

local scene = CCScene:create();
scene:addChild(createLayer());
CCDirector:sharedDirector():replaceScene(scene);



 

运行效果:

 【木头Cocos2d-x 008】在Lua中使用自定义类——tolua++工具使用(下集)_第1张图片

完美~!噗,自己赞自己。

 

 

 

你可能感兴趣的:(笨木头,Cocos2D-x,笨木头Cocos2d-x)