3 title: Cocos2d-x之使用tolua++生成lua对象
4 description: 以CCScale9Sprite为例
10 <div class="page-header">
11 <h3>一、创建CCScale9Sprite.pkg</h3>
13 <div class="lgf-command">
22 <pre class="lgf-command-description prettyprint">
23 class CCScale9Sprite : public CCNode
25 void setPreferredSize(CCSize size);
26 static CCScale9Sprite* create(const char *pszFileName);
32 <div class="page-header">
33 <h3>二、修改Cocos2d.pkg</h3>
35 <pre class="prettyprint">
36 $#include "LuaCocos2d.h"
38 $pfile "CCScale9Sprite.pkg"
43 <div class="page-header">
44 <h3>三、生成CCScale9Sprite.cpp</h3>
46 <div class="lgf-command">
47 执行 @./tolua++ -tCocos2d -o CCScale9Sprite.cpp Cocos2d.pkg@
48 把生成的内容拷贝到LuaCocos2d.cpp,注意位置
50 <pre class="lgf-command-description prettyprint">
51 // static void tolua_reg_types (lua_State* tolua_S)
52 tolua_usertype(tolua_S,"CCScale9Sprite");
54 /* method: setPreferredSize of class CCScale9Sprite */
55 #ifndef TOLUA_DISABLE_tolua_Cocos2d_CCScale9Sprite_setPreferredSize00
56 static int tolua_Cocos2d_CCScale9Sprite_setPreferredSize00(lua_State* tolua_S)
59 tolua_Error tolua_err;
61 !tolua_isusertype(tolua_S,1,"CCScale9Sprite",0,&tolua_err) ||
62 (tolua_isvaluenil(tolua_S,2,&tolua_err) || !tolua_isusertype(tolua_S,2,"CCSize",0,&tolua_err)) ||
63 !tolua_isnoobj(tolua_S,3,&tolua_err)
69 CCScale9Sprite* self = (CCScale9Sprite*) tolua_tousertype(tolua_S,1,0);
70 CCSize size = *((CCSize*) tolua_tousertype(tolua_S,2,0));
72 if (!self) tolua_error(tolua_S,"invalid 'self' in function 'setPreferredSize'", NULL);
75 self->setPreferredSize(size);
81 tolua_error(tolua_S,"#ferror in function 'setPreferredSize'.",&tolua_err);
85 #endif //#ifndef TOLUA_DISABLE
87 /* method: create of class CCScale9Sprite */
88 #ifndef TOLUA_DISABLE_tolua_Cocos2d_CCScale9Sprite_create00
89 static int tolua_Cocos2d_CCScale9Sprite_create00(lua_State* tolua_S)
92 tolua_Error tolua_err;
94 !tolua_isusertable(tolua_S,1,"CCScale9Sprite",0,&tolua_err) ||
95 !tolua_isstring(tolua_S,2,0,&tolua_err) ||
96 !tolua_isnoobj(tolua_S,3,&tolua_err)
102 const char* pszFileName = ((const char*) tolua_tostring(tolua_S,2,0));
104 CCScale9Sprite* tolua_ret = (CCScale9Sprite*) CCScale9Sprite::create(pszFileName);
105 tolua_pushusertype(tolua_S,(void*)tolua_ret,"CCScale9Sprite");
109 #ifndef TOLUA_RELEASE
111 tolua_error(tolua_S,"#ferror in function 'create'.",&tolua_err);
115 #endif //#ifndef TOLUA_DISABLE
117 // TOLUA_API int tolua_Cocos2d_open (lua_State* tolua_S)
118 tolua_cclass(tolua_S,"CCScale9Sprite","CCScale9Sprite","CCNode",NULL);
119 tolua_beginmodule(tolua_S,"CCScale9Sprite");
120 tolua_function(tolua_S,"setPreferredSize",tolua_Cocos2d_CCScale9Sprite_setPreferredSize00);
121 tolua_function(tolua_S,"create",tolua_Cocos2d_CCScale9Sprite_create00);
122 tolua_endmodule(tolua_S);
127 <div class="page-header">
128 <h3>四、修改LuaCocos2d.h,因为CCScale9Sprite对象在Cocos2d-x的扩展包中</h3>
130 <pre class="prettyprint">
131 #include "cocos-ext.h"
132 using namespace cocos2d::extension;
137 <div class="page-header">
140 <pre class="prettyprint">
141 local sprite = CCScale9Sprite:create("image.png")
142 sprite:setPreferredSize(CCSizeMake(100, 100))