【木頭Cocos2d-x 008】在Lua中使用自訂類——tolua++工具使用(下集)

來源:互聯網
上載者:User

在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_sharedSpriteFactory00static 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_RELEASEtolua_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_sharedSpriteFactory00static 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_createSprite00static 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;endlocal scene = CCScene:create();scene:addChild(createLayer());CCDirector:sharedDirector():replaceScene(scene);

 

運行效果:

 

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

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.