在Lua中使用自定义类——tolua++工具使用(下集)
来源:互联网 发布:php 求数组最大值 编辑:程序博客网 时间:2024/04/29 10:43
转载请注明,原文地址: 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_sharedSpriteFactory00
- static 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
知道我想说什么吧?既然它已经能生成自定类的这些代码了,那还怕什么。
现在回到生成LuaCocos2d.cpp的步骤,打开Cocos2d.pkg文件,把其它所有无关的类删除~!变成这样:
- <SPAN style="FONT-SIZE: 14px">$#include "LuaCocos2d.h"
- $pfile " SpriteFactory.pkg"
- </SPAN>
然后继续输入命令,生成LuaCocos2d.cpp文件。
于是,这样生成的文件就只有我们自定义类的声明代码了:
- <SPAN style="FONT-SIZE: 14px">#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_sharedSpriteFactory00
- static 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_createSprite00
- static 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
- </SPAN>
这就没有问题了,我们不要替换它原有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;
- end
- local scene = CCScene:create();
- scene:addChild(createLayer());
- CCDirector:sharedDirector():replaceScene(scene);
运行效果:
- 在Lua中使用自定义类——tolua++工具使用(下集)
- 在Lua中使用自定义类——tolua++工具使用(下集)
- 【木头Cocos2d-x 008】在Lua中使用自定义类——tolua++工具使用(下集)
- 在Lua中使用自定义类——tolua++工具使用(上集)
- 在Lua中使用自定义类——tolua++工具使用(上集)For Windows
- 在Lua中使用自定义类——tolua++工具使用
- 【木头Cocos2d-x 007】在Lua中使用自定义类——tolua++工具使用(上集)
- 【木头Cocos2d-x 007】在Lua中使用自定义类——tolua++工具使用(上集)
- cocos2dx lua中使用自定义类以及tolua++的使用
- cocos2dx lua中使用自定义类以及tolua++的使用
- cocos2d-x tolua++使用(C++自定义类导入lua)
- tolua++使用(将2d_x自定义类导入lua)
- ——tolua++使用(将2d_x自定义类_CursorTextField_导入lua)
- 使用cocos2d-x tolua++工具导出自定义类到lua脚本环境
- cocos2dx-lua中如何使用自定义类以及tolua++的使用
- cocos2dx-lua中如何使用自定义类以及tolua++的使用
- cocos2dx-lua中如何使用自定义类以及tolua++的使用
- Cocos2dx-lua中如何使用自定义类以及tolua++的使用
- Z字蛇形数组的赋值
- 在fedora19上使用4个第三方源
- 推荐十款来自极客标签的超棒前端特效[第十四期]
- android基础--ProgressBar实例
- HDU4374
- 在Lua中使用自定义类——tolua++工具使用(下集)
- sublime text 2实现C艹配置
- 开blog第一天 想想自己的专业
- 同步和异步有何异同
- 最小生成树的算法
- Burnside引理和polay计数学习小记
- 理解Javascript的闭包【讲得很清楚,推荐看】
- GET与POST区别
- hdu 2147 kiki's game 简单博弈