前端之家收集整理的这篇文章主要介绍了
cocos2dx tolua传递参数分析,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
cocos2dx tolua传递参数分析:
tolua_Cocos2d_CCNode_addChild00 == void CCNode::addChild(CCNode *child)
tolua_Cocos2d_CCNode_addChild01 == void CCNode::addChild(CCNode *child,int zOrder)
tolua_Cocos2d_CCNode_addChild02 == void CCNode::addChild(CCNode *child,int zOrder,int tag)
上面是使用tolua把C++转到lua,上面三个分别对应不同的参数个数。我们这里分析一下最后一个:
/* method: addChild of class CCNode */
#ifndef TOLUA_DISABLE_tolua_Cocos2d_CCNode_addChild02
static int tolua_Cocos2d_CCNode_addChild02(lua_State* tolua_S)
{
tolua_Error tolua_err;
//这里判断参数是否合法,只能判断参数类型是否合法,如果参数类型检查不通过,就会报错
if (
!tolua_isusertype(tolua_S,1,"CCNode",&tolua_err) ||
!tolua_isusertype(tolua_S,2,&tolua_err) ||
!tolua_isnumber(tolua_S,3,4,&tolua_err) ||
!tolua_isnoobj(tolua_S,5,&tolua_err)
)
goto tolua_lerror;
else
{
//例如:middleBg:addChild(testSprite,Z_ORDER_1,200)
//我们传递进来的参数和下面的一一对应
//这里可以清楚的看到传入参数的次序,第一是middleBg ...
CCNode* self = (CCNode*) tolua_tousertype(tolua_S,0); -- middleBg -- 1
CCNode* child = ((CCNode*) tolua_tousertype(tolua_S,0)); -- testSprite -- 2
int zOrder = ((int) tolua_tonumber(tolua_S,0)); -- Z_ORDER_1 -- 3
int tag = ((int) tolua_tonumber(tolua_S,0)); -- 200 -- 4
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'addChild'",NULL);
#endif
{
//这里就把testSprite加到了middleBg
self->addChild(child,zOrder,tag);
}
}
return 0;
tolua_lerror:
return tolua_Cocos2d_CCNode_addChild01(tolua_S);
}
#endif //#ifndef TOLUA_DISABLE
例子:
//middleBg:addChild(testSprite,200) 在传递参数中,我们没有对Z_ORDER_1赋值,即为nil,则就
//出现了下面的错误,看下这里argument #3 ,表明是第三个参数错误,而这里我们明明是第二个参数,看下上面的分析
//我想大家应该明白了,通过这种方法,我们以后查找这类问题,就会方便很多。
03-31 10:09:50.499: D/cocos2d-x debug info(3169): LUA ERROR: [string "xxxxxxxxxxxxx..."]:171: error in function 'addChild'.
03-31 10:09:50.499: D/cocos2d-x debug info(3169): argument #3 is 'nil'; '[no object]' expected.
03-31 10:09:50.499: D/cocos2d-x debug info(3169): stack traceback:
03-31 10:09:50.499: D/cocos2d-x debug info(3169): [C]: in function 'addChild'
03-31 10:09:50.499: D/cocos2d-x debug info(3169): [string "xxxxxxxxxxxxx..."]:171: in function 'initMiddle'
03-31 10:09:50.499: D/cocos2d-x debug info(3169): [string "xxxxxxxxxxxxx..."]:35: in function 'initUI'
03-31 10:09:50.499: D/cocos2d-x debug info(3169): [string "xxxxxxxxxxxxx..."]:27: in function 'ctor'
03-31 10:09:50.499: D/cocos2d-x debug info(3169): [string "xxxxxxxxxxxxx"]:34: in function 'create'
03-31 10:09:50.499: D/cocos2d-x debug info(3169): [string "xxxxxxxxxxxxx"]:38: in function 'new'
03-31 10:09:50.499: D/cocos2d-x debug info(3169): [string "xxxxxxxxxxxxx"]:44: in function 'pushScene'
03-31 10:09:50.499: D/cocos2d-x debug info(3169): [string "xxxxxxxxxxxxx"]:309: in function 'callBack'
03-31 10:09:50.499: D/cocos2d-x debug info(3169): [string "xxxxxxxxxxxxx"]:653: in function <[string "xxxxxxxxxxxxx"]:651>