CCMenuItem tag

CCMenuItem tag
0.0 0



I am trying to catch all the menu events using a method that will identify which menu item was tapped based on its tag. However, in the callback method the tag is some random number, and not the number that I set.

For example:

  1. create a new cocos2d-x project
  2. in HelloWorld::init() add pCloseItem->setTag(23);
  3. update the menuCloseCallback to receive (CCMenuItemImage* pSender) as parameter in the header and the implementation file
  4. set a breakpoint inside the menuCloseCallback method

When the breakpoint is hit, run p pSender->getTag() in the debugger. Instead of getting 23, I am getting some random number.

Am I doing something wrong?



I think, that you have wrong callback method parametr. It must be CCObject**.
Right is:
void menuCloseCallback
CCNode** node = (CCNode*) pSender;
int tag = node->getTag();


You’re right!

I assumed that since pSender is in fact a CCMenuItemImage* object I can just swap it with CCObject* in the method definition and have it work automatically.

C++ is pretty dumb for a smart language.