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

来源:互联网 发布:php 求数组最大值 编辑:程序博客网 时间:2024/04/29 10:43
转载请注明,原文地址 http://blog.csdn.net/musicvs/article/details/8166655 

 

正文:

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

 

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

 

 

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

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

[cpp] view plaincopyprint?
  1. /* method: sharedSpriteFactory of class  SpriteFactory */  
  2. #ifndef TOLUA_DISABLE_tolua_Cocos2d_SpriteFactory_sharedSpriteFactory00  
  3. static int tolua_Cocos2d_SpriteFactory_sharedSpriteFactory00(lua_State* tolua_S)  
  4. {  
  5. #ifndef TOLUA_RELEASE   
  6.     tolua_Error tolua_err;  
  7.     if (  
  8.         !tolua_isusertable(tolua_S,1,"SpriteFactory",0,&tolua_err) ||  
  9.         !tolua_isnoobj(tolua_S,2,&tolua_err)  
  10.         )  
  11.         goto tolua_lerror;  
  12.     else  
  13. #endif   
  14.     {  
  15.         {  
  16.             SpriteFactory* tolua_ret = (SpriteFactory*)  SpriteFactory::sharedSpriteFactory();  
  17.             tolua_pushusertype(tolua_S,(void*)tolua_ret,"SpriteFactory");  
  18.         }  
  19.     }  
  20.     return 1;  
  21. #ifndef TOLUA_RELEASE   
  22. tolua_lerror:  
  23.     tolua_error(tolua_S,"#ferror in function 'sharedSpriteFactory'.",&tolua_err);  
  24.     return 0;  
  25. #endif   
  26. }  
  27. #endif //#ifndef TOLUA_DISABLE  

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

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

[cpp] view plaincopyprint?
  1. <SPAN style="FONT-SIZE: 14px">$#include "LuaCocos2d.h"  
  2.   
  3. $pfile " SpriteFactory.pkg"  
  4. </SPAN>  


 

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

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

[cpp] view plaincopyprint?
  1. <SPAN style="FONT-SIZE: 14px">#include "LuaCocos2d.h"  
  2.   
  3. /* function to register type */  
  4. static void tolua_reg_types (lua_State* tolua_S)  
  5. {  
  6. #ifndef Mtolua_typeid   
  7. #define Mtolua_typeid(L,TI,T)  
  8. #endif   
  9.  tolua_usertype(tolua_S,"CCSprite");  
  10.  Mtolua_typeid(tolua_S,typeid(CCSprite), "CCSprite");  
  11.  tolua_usertype(tolua_S,"CCLayer");  
  12.  Mtolua_typeid(tolua_S,typeid(CCLayer), "CCLayer");  
  13.  tolua_usertype(tolua_S,"SpriteFactory");  
  14.  Mtolua_typeid(tolua_S,typeid(SpriteFactory), "SpriteFactory");  
  15. }  
  16.   
  17. /* method: sharedSpriteFactory of class  SpriteFactory */  
  18. #ifndef TOLUA_DISABLE_tolua_Cocos2d_SpriteFactory_sharedSpriteFactory00  
  19. static int tolua_Cocos2d_SpriteFactory_sharedSpriteFactory00(lua_State* tolua_S)  
  20. {  
  21. #ifndef TOLUA_RELEASE   
  22.  tolua_Error tolua_err;  
  23.  if (  
  24.      !tolua_isusertable(tolua_S,1,"SpriteFactory",0,&tolua_err) ||  
  25.      !tolua_isnoobj(tolua_S,2,&tolua_err)  
  26.  )  
  27.   goto tolua_lerror;  
  28.  else  
  29. #endif   
  30.  {  
  31.   {  
  32.    SpriteFactory* tolua_ret = (SpriteFactory*)  SpriteFactory::sharedSpriteFactory();  
  33.     tolua_pushusertype(tolua_S,(void*)tolua_ret,"SpriteFactory");  
  34.   }  
  35.  }  
  36.  return 1;  
  37. #ifndef TOLUA_RELEASE   
  38.  tolua_lerror:  
  39.  tolua_error(tolua_S,"#ferror in function 'sharedSpriteFactory'.",&tolua_err);  
  40.  return 0;  
  41. #endif   
  42. }  
  43. #endif //#ifndef TOLUA_DISABLE  
  44.   
  45. /* method: createSprite of class  SpriteFactory */  
  46. #ifndef TOLUA_DISABLE_tolua_Cocos2d_SpriteFactory_createSprite00  
  47. static int tolua_Cocos2d_SpriteFactory_createSprite00(lua_State* tolua_S)  
  48. {  
  49. #ifndef TOLUA_RELEASE   
  50.  tolua_Error tolua_err;  
  51.  if (  
  52.      !tolua_isusertype(tolua_S,1,"SpriteFactory",0,&tolua_err) ||  
  53.      !tolua_isusertype(tolua_S,2,"CCLayer",0,&tolua_err) ||  
  54.      !tolua_isnumber(tolua_S,3,0,&tolua_err) ||  
  55.      !tolua_isnoobj(tolua_S,4,&tolua_err)  
  56.  )  
  57.   goto tolua_lerror;  
  58.  else  
  59. #endif   
  60.  {  
  61.   SpriteFactory* self = (SpriteFactory*)  tolua_tousertype(tolua_S,1,0);  
  62.   CCLayer* mLayer = ((CCLayer*)  tolua_tousertype(tolua_S,2,0));  
  63.   SpriteFactory::SpriteType enSpriteType = ((SpriteFactory::SpriteType) (int)  tolua_tonumber(tolua_S,3,0));  
  64. #ifndef TOLUA_RELEASE   
  65.   if (!self) tolua_error(tolua_S,"invalid 'self' in function 'createSprite'", NULL);  
  66. #endif   
  67.   {  
  68.    CCSprite* tolua_ret = (CCSprite*)  self->createSprite(mLayer,enSpriteType);  
  69.     tolua_pushusertype(tolua_S,(void*)tolua_ret,"CCSprite");  
  70.   }  
  71.  }  
  72.  return 1;  
  73. #ifndef TOLUA_RELEASE   
  74.  tolua_lerror:  
  75.  tolua_error(tolua_S,"#ferror in function 'createSprite'.",&tolua_err);  
  76.  return 0;  
  77. #endif   
  78. }  
  79. #endif //#ifndef TOLUA_DISABLE  
  80.   
  81. /* Open function */  
  82. TOLUA_API int tolua_Cocos2d_open (lua_State* tolua_S)  
  83. {  
  84.  tolua_open(tolua_S);  
  85.  tolua_reg_types(tolua_S);  
  86.  tolua_module(tolua_S,NULL,0);  
  87.  tolua_beginmodule(tolua_S,NULL);  
  88.   tolua_cclass(tolua_S,"SpriteFactory","SpriteFactory","",NULL);  
  89.   tolua_beginmodule(tolua_S,"SpriteFactory");  
  90.    tolua_constant(tolua_S,"en_close",SpriteFactory::en_close);  
  91.    tolua_constant(tolua_S,"en_grossini",SpriteFactory::en_grossini);  
  92.    tolua_constant(tolua_S,"en_grossinis_sister",SpriteFactory::en_grossinis_sister);  
  93.    tolua_constant(tolua_S,"en_grossinis_sister2",SpriteFactory::en_grossinis_sister2);  
  94.    tolua_function(tolua_S,"sharedSpriteFactory",tolua_Cocos2d_SpriteFactory_sharedSpriteFactory00);  
  95.    tolua_function(tolua_S,"createSprite",tolua_Cocos2d_SpriteFactory_createSprite00);  
  96.   tolua_endmodule(tolua_S);  
  97.  tolua_endmodule(tolua_S);  
  98.  return 1;  
  99. }  
  100.   
  101.   
  102. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501  
  103.  TOLUA_API int luaopen_Cocos2d (lua_State* tolua_S) {  
  104.  return tolua_Cocos2d_open(tolua_S);  
  105. };  
  106. #endif   
  107. </SPAN>  


 

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

 

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

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

[plain] view plaincopyprint?
  1. local function createLayer()  
  2.     local layer = CCLayer:create();  
  3.       
  4.     local sprite = SpriteFactory:sharedSpriteFactory():createSprite(layer, SpriteFactory.en_grossini);  
  5.     sprite:setPosition(200, 200);  
  6.     return layer;  
  7. end  
  8.   
  9. local scene = CCScene:create();  
  10. scene:addChild(createLayer());  
  11. CCDirector:sharedDirector():replaceScene(scene);  



 

运行效果:

 

原创粉丝点击