BUG onEnter() and CCMenuItems

BUG onEnter() and CCMenuItems
0.0 0


I don’t know if I’m doing something wrong or if I don’t understand some concept here; but I had a problem with buttons (they didn’t respond) and it was because there were a virtual void onEnter() on the class.
Here is the relevant code:


virtual bool       init();

virtual void       onEnter(); // if I delete this, button works fine.
virtual void       onExit();
void               onMenuDn(CCObject* sender);

CCMenuItemSprite*  menu_btn;
CCMenu*            menu;


bool Class::init()
CCSprite* spriteOn = .....;
CCSprite* spriteOff = .....;

menu_btn = CCMenuItemSprite::create(spriteOff, spriteOn, this, menu_selector(Class::onMenuDn));

menu = CCMenu::create(menu_btn, NULL);

return true;

void Class::onEnter()

void Class::onMenuDn(CCObject* sender)
CCLog("Button pressed");

With onEnter() the button doesn’t work, but if take out the onEnter method the button works fine. Why is this happening? Is this a bug? or am I missing something?

Cocos version: cocos2d-x-2.2





When overloading virtual function, it is best to call the base class function inside your own. You can see this pattern from the default code where HelloWorld::init() calls the base class Layer::init() inside its implementation. Cocos virtual functions have some code that needs to be run in order for things to work.

In your code, if you want to overload onEnter() function, make sure to call the onEnter() function of the base class.

void Class::onEnter()
    // your code goes here.

Hope this help.