在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檔案,然後,編譯~!
大功告成……啊才怪啊~!
接下來發生什麼事?請不要轉台,關注下集最新情況~