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

來源:互聯網
上載者:User

在Lua中使用自訂類——tolua++工具使用(上集)

笨木頭花心貢獻,啥?花心?不呢,是用心~

轉載請註明,原文地址: http://blog.csdn.net/musicvs/article/details/8166572 

 

本文:

 

最近看了一下TestLua的例子,挺有意思的,使用Lua對網遊開發來說,很實用。我目前這個項目沒有使用Lua等指令碼,已經吃盡苦頭了,每次用戶端更新就流失好多玩家。雖然我的項目沒有用cocos2d-x開發,但是因為最近在研究它,所以就好奇一下lua和cocos2d-x的整合。

百度沒有多少資料,最實在的一篇教程應該就是Himi那篇了(高手一隻,不解釋)。所以只能靠自己了。

怎麼運行Lua例子,以及在cocos2d-x裡使用Lua的簡單規則,請看之前我寫的那篇教程,以及更加詳細的Himi的教程(http://www.himigame.com/iphone-cocos2dx/681.html)。

這裡我主要是想分享一下如何在lua中使用自己的類,其實cocos2d-x已經提供工具,並且封裝好了c++整合lua。

好,廢話說多了,開始~!

 

1.   只關注LuaCocos2d.cpp檔案

假設看這篇文章的朋友已經瞭解了LuaCocos2d.cpp的大概作用(不瞭解的,可以看看上面推薦的那篇Himi的文章),我們知道,要在lua使用cocos2d-x裡的類,就首先要在LuaCocos2d.cpp裡“聲明”,此聲明非彼聲明哈,但是比較貼切。反正就是要在這個LuaCocos2d.cpp裡做文章就是了。雖然這個cpp檔案很龐大,但是都是一些重複的東西,形如:

tolua_cclass(tolua_S,"CCActionEase","CCActionEase","CCActionInterval",NULL);  tolua_beginmodule(tolua_S,"CCActionEase");   tolua_function(tolua_S,"copyWithZone",tolua_Cocos2d_CCActionEase_copyWithZone00);   tolua_function(tolua_S,"reverse",tolua_Cocos2d_CCActionEase_reverse00);   tolua_function(tolua_S,"create",tolua_Cocos2d_CCActionEase_create00);  tolua_endmodule(tolua_S);

這些操作就像是在向程式註冊我們的類,讓這些類可以在lua中使用。

 

2.   真正想知道的事情是,除了cocos2d-x的類,我們可以在lua中使用自己的類嗎?

當然可以了,就算不是在cocos2d-x引擎裡,本身lua就可以在c++中互相調用的(當然,Java也可以)。只是,既然cocos2d-x幫我們封裝好了,我們就用它提供的方法來使用lua吧。

首先,我第一時間會想到,在LuaCocos2d.cpp中依葫蘆畫瓢,把自己的類加進去就好了。當然,我也是這麼想的,但是在添加的過程中碰了釘子,很多規則不太清楚,總是有些錯誤。後來。。。

 

3.   tolua++.exe工具

後來我發現了cocos2d-x竟然提供了這個工具,這是十分難發現的,隱藏得很深的,沒有任何提示的,我竟然能發現它,噗。

(我才不會告訴你們LuaCocos2d.cpp檔案的頂部有注釋,並且注釋已經告訴我們有這個工具的)

/*** Lua binding: Cocos2d** Generated automatically by tolua++-1.0.92 on 08/30/12 12:11:53.*/

工具在這個路徑下:cocos2d-2.0-x-2.0.2\tools\tolua++,有windows版本的,和mac版本的。

這個工具可以幫我們產生自訂類的“聲明”代碼。怎麼使用?該目錄下有個README檔案,我用我那蹩腳的英語水平看懂了80%,並且加上了蹩腳的中文注釋,希望不要介意:

1.Generating the lua<-->C bindings with tolua++

//使用此命令產生LuaCocos2d.cpp檔案

tolua++.exe -tCocos2d -o LuaCocos2d.cpp Cocos2d.pkg                 

   An ant script has been provided to generate the relevant files, to do this after

   modifying the .pkg files you should use the following command in this directory:

   ant

   This will generate the bindings file, patch it to compile successfully and move it

   to the standard destination.

 

2. Writing .pkg files                 //為要在lua使用的類編寫pkg檔案

   1) enum keeps the same     //枚舉類型保留不變

   2) remove CC_DLL for the class defines, pay attention to multi inherites      //不要使用CC_DLL,改用多繼承

   3) remove inline keyword for declaration and implementation                //刪除內建變數?

   4) remove public protect and private     //不要用訪問限定詞

   5) remove the decalration of class member variable               //不要成員變數

   6) keep static keyword         //保留靜態關鍵詞

   7) remove memeber functions that declared as private or protected //非public的函數都刪除

 

也許有點混亂,來看看該目錄下的這個檔案:Cocos2d.pkg

$#include "LuaCocos2d.h"

 

$pfile "CCAction.pkg"

$pfile "CCActionCamera.pkg"

$pfile "CCActionCatmullRom.pkg"

$pfile "CCActionEase.pkg"

$pfile "CCActionGrid.pkg"

$pfile "CCActionGrid3D.pkg"

$pfile "CCActionManager.pkg"

 

其實這些就是cocos2d-x的一些,這些類放在這裡,是為了自動產生LuaCocos2d.cpp檔案,這樣,在lua中就可以使用這些類了。

那麼,我們同樣可以把自訂的類放到這裡來。

當然,必須按照規則來編寫pkg檔案。

別著急,一步步來…

 

4.   編寫pkg檔案

a.      
首先我寫了一個自訂的類,這個類很簡單,用來產生精靈的工廠類:

/************************************************************************//* 精靈工廠                                   *//* 使用了簡單工廠,但,不是原廠模式           *//************************************************************************/#ifndef __SPRITE_FACTORY_H__#define __SPRITE_FACTORY_H__#include "cocos2d.h"using namespace cocos2d;class SpriteFactory{public:    enum SpriteType    {        en_close,        en_grossini,        en_grossinis_sister,        en_grossinis_sister2,    };    static SpriteFactory* sharedSpriteFactory();    CCSprite* createSprite(CCLayer* mLayer, SpriteType enSpriteType);private:    static SpriteFactory* mFactory;};#endif

 

b.      
然後,開始編寫pkg檔案,還記得README裡的規則嗎?再看一次:

   1) enum keeps the same    //枚舉類型保留不變

   2) remove CC_DLL for the class defines, pay attention to multi inherites      //不要使用CC_DLL,改用多繼承

   3) remove inline keyword for declaration and implementation                //刪除內建變數?

   4) remove public protect and private     //不要用訪問限定詞

   5) remove the decalration of class member variable               //不要成員變數

   6) keep static keyword         //保留靜態關鍵詞

   7) remove memeber functions that declared as private or protected //非public的函數

        

根據這個規則,我要把public和private關鍵字去掉,還要把所有成員變數去掉,當然,多餘的什麼include、using namespace都不要了,對了,枚舉類型要保留。

於是,最後的pkg檔案如下:

class SpriteFactory{    enum SpriteType    {        en_close,        en_grossini,        en_grossinis_sister,        en_grossinis_sister2,    };    static SpriteFactory* sharedSpriteFactory();    CCSprite* createSprite(CCLayer* mLayer, SpriteType enSpriteType);};

 

好簡潔,我喜歡。

         OK,現在來試試產生LuaCocos2d.cpp檔案吧,開啟cmd,進入tolua++.exe工具的目錄,把我們的SpriteFactory.pkg檔案也拷到這個目錄,啊~!對了!要在Cocos2d.pkg裡加上SpriteFactory.pkg檔案:

         $pfile " SpriteFactory.pkg"

         然後,在cmd裡輸入命令,開始產生LuaCocos2d.cpp檔案~

OK,沒有意外的話,LuaCocos2d.cpp已經在目前的目錄下了,把它拷到你的lua工程裡,替換掉原來的LuaCocos2d.cpp檔案,然後,編譯~!

 

 

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

 

 

接下來發生什麼事?請不要轉檯,關注下集最新情況~

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.