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",0,&tolua_err) || !tolua_isusertype(tolua_S,2,"CCNode",0,&tolua_err) || !tolua_isnumber(tolua_S,3,0,&tolua_err) || !tolua_isnumber(tolua_S,4,0,&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,1,0); -- middleBg -- 1 CCNode* child = ((CCNode*) tolua_tousertype(tolua_S,2,0)); -- testSprite -- 2 int zOrder = ((int) tolua_tonumber(tolua_S,3,0)); -- Z_ORDER_1 -- 3 int tag = ((int) tolua_tonumber(tolua_S,4,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, Z_ORDER_1, 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>
版权声明:原创文章,转载请注明出处。