Error in function 'addChild'. argument #2 is 'CCxxx'; 'CCMenuItem' expected

We use cocos2d-x+lua to develop mobile game. Most of time it runs ok. But there is a strange problem, that is when i add some CCObject such as CCSprite, CCLayer, CCNode … to the other CCObject such as CCLayer,CCNode … sometimes it runs ok, but sometimes it cause the error: error in function ‘addChild’. argument #2 is ‘CCxxx’; ‘CCMenuItem’ expected. the CCxxx may be CCLayerColor, CCSprite, CCNode …, I really don’t found the reason of the problem, and the lua syntax looks right(because the game runs good until the problem appear). anybody can give me some ideas? I am deeply appreciative of any ideas.

1 Like

I found the problem raise only when add child to CCLayer
nature wang wrote:

We use cocos2d-x+lua to develop mobile game. Most of time it runs ok. But there is a strange problem, that is when i add some CCObject such as CCSprite, CCLayer, CCNode … to the other CCObject such as CCLayer,CCNode … sometimes it runs ok, but sometimes it cause the error: error in function ‘addChild’. argument #2 is ‘CCxxx’; ‘CCMenuItem’ expected. the CCxxx may be CCLayerColor, CCSprite, CCNode …, I really don’t found the reason of the problem, and the lua syntax looks right(because the game runs good until the problem appear). anybody can give me some ideas? I am deeply appreciative of any ideas.

Did you try the tolua.cast() to cast object to the exactly type?

You can try it and check if it were working correctly.

You need check the object which call the addchild function.if the object is CCMenu, the argument #2 of addchild must be is ‘CCMenuItem’ .

I add child to CCLayer, but it call addChild method of CCMenu, and cause the error. CCMenu inherit from CCLayer, is this the reason ?
samuele3 hu wrote:

You need check the object which call the addchild function.if the object is CCMenu, the argument #2 of addchild must be is ‘CCMenuItem’ .

How your CCLayer create?After CCLayer created,did you do some cast operator to the object before addchild?

No, I didn’t cast CCLayer to CCMenu. I look the code of LuaCocos2d.cpp which generate by tolua++, i doubt may be the metatable of CCMenu rewrite metatable of CCLayer, and some method such as addChild of CCLayer be rewrited.
samuele3 hu wrote:

How your CCLayer create?After CCLayer created,did you do some cast operator to the object before addchild?

1 Like

No, I didn’t cast CCLayer to CCMenu. I look the code of LuaCocos2d.cpp which generate by tolua++, i doubt may be the metatable of CCMenu rewrite metatable of CCLayer, and some method such as addChild of CCLayer be rewrited.
samuele3 hu wrote:

How your CCLayer create?After CCLayer created,did you do some cast operator to the object before addchild?

En,the metatable maybe rewrite.Can you paste the lua code which cause this error.

samuele3 hu,can you add my qq:423827921 or give me your qq! i have the same question.i need your help,thanks !

look at your pkg files, the subclass pkg file should position after parent class

I think so,I have the same situation too.“addChild” adopt a gangster for a father, but how to solve the question?How can I choose and use the right “addChid”?By the way, my project changed some base code, such as ccmenuitem’s action, when u touch down the menu, it will act an action, maybe it is one of the reason caused the question, could u tell me how u solved the question? 3q very much

hi ,have you solved the problem and have any processing method?