此篇可能會在最新的cocos2dx版本中出現如下問題:
1
2
lua error: ...24f82-1230-41fe-8a04-c445fb7d1bab/mtet.app/hello.lua:35:
error in function 'addchild'. argument #2 is 'mysprite'; 'ccnode' expected.
最近himi都沒有更新博文了,其實也是猶豫寫本cocos2d(x)引擎書籍在做準備,目錄的草稿都寫好了,目錄中包含的大家最感興趣的cocos2d/x動作編輯器的詳細制作流程與源碼! 但是遺憾的是himi還是騰不出時間去寫;
放棄去寫的另外一個原因就是因為支援我的童鞋門,在7月份就說過要為大家奉上關于cocos2dx-lua的系列教程,但是一直由于時間等問題一再拖到現在,如果himi真要準備寫書估計半年内都基本很難有時間更新部落格的,當然也考慮到公司項目,最終放棄; :xl:
順便說一句,關于himi9個技術群,不論是cocos2d-iphone、cocos2dx、android還是以後陸續公布的unity3d群,周期性的himi和管理者們會定期清理(一切都是為了更多想學習的新童鞋考慮),希望大家進群冒泡,積極讨論。好了,廢話就不多說了,從今天開始himi為童鞋們出cocos2dx-lua的系列開發教程,希望大家還一如既往得支援;3q :bb:
himi 目前開發工具等版本如下:
mac: 10.8 xcode:4.4.1 cocos2dx :cocos2d-2.0-rc2-x-2.0.1
本篇介紹兩個知識點: 1. lua基礎 2.在lua腳本中使用我們自定義的精靈類
一:lua基礎
二:在lua腳本中使用我們自定義的精靈類
首先建立cocos2dx-lua項目,然後在項目中添加我們的自定義精靈類:這裡himi類名為:hsprite
hsprite.h:
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//
// hsprite.h
// cocos2dx_lua_tests_by_himi
// created by himi on 12-8-30.
#ifndef cocos2dx_lua_tests_by_himi_hsprite_h
#define cocos2dx_lua_tests_by_himi_hsprite_h
#include "cocos2d.h"
using namespace cocos2d;
class hsprite : public cocos2d::ccsprite{
public:
static hsprite* createhsprite(const char* _name);
void hspriteinit();
};
#endif
hsprite.cpp:
22
23
24
// hsprite.cpp
#import "hsprite.h"
hsprite* hsprite::createhsprite(const char* _name){
hsprite* sp = new hsprite();
if(sp && sp->initwithfile(_name)){
sp->hspriteinit();
sp->autorelease();
return sp;
}
cc_safe_delete(sp);
return null;
}
void hsprite::hspriteinit(){
ccmessagebox("create hsprite success", "himi_lua");
以上代碼不做解釋了,很簡單,繼承ccsprite,添加一個自動釋放的建立函數(createhsprite)以及一個自定義初始化函數(hspriteinit)
下面我們打開luacocos2d.cpp 類,這個類在項目的 libs/lua/cocos2dx_support目錄下,如下圖:

然後開始添加我們自定義精靈類,讓lua腳本能認識它;
步驟分為3步:
1. 注冊我們的自定義類:
在luacocos2d.cpp類中搜尋“tolua_reg_types”這個函數,然後在其中進行注冊:
tolua_usertype(tolua_s,"hsprite");
如下圖所示:
第二步:聲明我們自定義類的函數:
搜尋“tolua_cocos2d_open”這個函數,然後在其中添加如下代碼:
tolua_cclass(tolua_s, "hsprite", "hsprite", "ccsprite", null);
tolua_beginmodule(tolua_s,"hsprite");
tolua_function(tolua_s,"createhsprite",tolua_himi_hsprite_createhsrpite00);
tolua_endmodule(tolua_s);
如下圖:
這裡開始解釋:
首先定義能讓腳本認識的類函數,遵循如下:
a) tolua_cclass(tolua_s, “hsprite”, “hsprite”, “ccsprite”, null);
tolua_cclass聲明哪個類函數,第一個狀态值預設:tolua_s
後兩個參數:是自定義類類名
再往後是繼承的父類類名
b)添加參數開始聲明:
tolua_beginmodule(tolua_s,”hsprite”);
c) 添加自定類函數:
tolua_function(tolua_s,”createhsprite”,tolua_himi_hsprite_createhsrpite00);
第一個參數預設,第二個參數自定義類名,第三個:實作腳本與自定義類之間的轉換實作函數
注意,這裡有多個函數,可以繼續寫;
d) 結束自定義函數:
tolua_endmodule(tolua_s);
第三步:實作我們的腳本之間轉換函數 tolua_himi_hsprite_createhsrpite00
實作如下:
25
26
27
28
29
30
31
/* method: create of class hsprite */
#ifndef tolua_disable_tolua_himi_hsprite_createhsrpite00
static int tolua_himi_hsprite_createhsrpite00(lua_state* tolua_s)
{
#ifndef tolua_release
tolua_error tolua_err;
if (
!tolua_isusertable(tolua_s,1,"hsprite",0,&tolua_err) ||
!tolua_isstring(tolua_s,2,0,&tolua_err) ||
!tolua_isnoobj(tolua_s,3,&tolua_err)
)
goto tolua_lerror;
else
{
const char* pszfilename = ((const char*) tolua_tostring(tolua_s,2,0));
{
hsprite* tolua_ret = (hsprite*) hsprite::createhsprite(pszfilename);
int nid = (tolua_ret) ? tolua_ret->m_uid : -1;
int* pluaid = (tolua_ret) ? &tolua_ret->m_nluaid : null;
tolua_pushusertype_ccobject(tolua_s, nid, pluaid, (void*)tolua_ret,"hsprite");
}
return 1;
tolua_lerror:
tolua_error(tolua_s,"#ferror in function 'create'.",&tolua_err);
return 0;
#endif //#ifndef tolua_disable
這裡himi解釋下:
童鞋們可以從第 384行的 #endif 把這個實作函數分為兩部分來看,
首先是375~384之間的代碼:
這裡是對參數類型的判斷:
tolua_isusertable 是否為”第三個參數”自定義類型
tolua_isstring 是否為字元串類型
tolua_isnoobj 結束(沒有參數的判斷)
然後是386~392之間的代碼段:
const char* pszfilename = ((const char*) tolua_tostring(tolua_s,2,0));
這裡是對腳本代碼的解析進而調用的自定義建立的函數
最後我們修改 hello2.lua 腳本中的代碼:(建立cocos2dx-lua項目預設resources下自帶的檔案)
在腳本最後”– run”下代碼修改如下:
-- run
local scenegame = ccscene:create()
-- scenegame:addchild(createlayerfram())
-- scenegame:addchild(createlayermenu())
scenegame:addchild(createhimilayer())
ccdirector:shareddirector():runwithscene(scenegame)
這裡himi注視了另個layer的添加,添加了自己的layer
然後将himi自定義方法添加在腳本中,代碼如下:
local function createhimilayer()
local layerh = cclayer:create()
local _font = cclabelttf:create("himi_(cocos2dx-lua)教程","arial",33)
_font:setposition(230,280)
layerh:addchild(_font)
--建立自定義類型精靈
local hsprite = hsprite:createhsprite("himi.png")
hsprite:setposition(100,100)
hsprite:setscale(1.5)
hsprite:setrotation(45)
layerh:addchild(hsprite)
return layerh
end
建立自己定義的精靈,然後進行調用縮放,旋轉,設定坐标函數。
ok,運作後的接圖如下:
好了,本篇就到這裡,童鞋們感覺本站好的文章記得分享,轉播散播哦~