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


#1

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.


#2

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.


#3

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

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


#4

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


#5

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’ .


#6

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


#7

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?


#8

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?


#9

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


#10

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


#11

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


#13

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


#14

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