diff --git a/cocos/2d/CCMenu.cpp b/cocos/2d/CCMenu.cpp index 3b0cefbec3..6fef9d78a6 100644 --- a/cocos/2d/CCMenu.cpp +++ b/cocos/2d/CCMenu.cpp @@ -149,7 +149,6 @@ bool Menu::initWithArray(Array* pArrayOfItems) } } - // [self alignItemsVertically]; _selectedItem = NULL; _state = Menu::State::WAITING; @@ -157,6 +156,17 @@ bool Menu::initWithArray(Array* pArrayOfItems) setCascadeColorEnabled(true); setCascadeOpacityEnabled(true); + + auto touchListener = EventListenerTouchOneByOne::create(); + touchListener->setSwallowTouches(true); + + touchListener->onTouchBegan = CC_CALLBACK_2(Menu::onTouchBegan, this); + touchListener->onTouchMoved = CC_CALLBACK_2(Menu::onTouchMoved, this); + touchListener->onTouchEnded = CC_CALLBACK_2(Menu::onTouchEnded, this); + touchListener->onTouchCancelled = CC_CALLBACK_2(Menu::onTouchCancelled, this); + + _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); + return true; } return false; @@ -184,16 +194,6 @@ void Menu::addChild(Node * child, int zOrder, int tag) void Menu::onEnter() { Layer::onEnter(); - - auto touchListener = EventListenerTouchOneByOne::create(); - touchListener->setSwallowTouches(true); - - touchListener->onTouchBegan = CC_CALLBACK_2(Menu::onTouchBegan, this); - touchListener->onTouchMoved = CC_CALLBACK_2(Menu::onTouchMoved, this); - touchListener->onTouchEnded = CC_CALLBACK_2(Menu::onTouchEnded, this); - touchListener->onTouchCancelled = CC_CALLBACK_2(Menu::onTouchCancelled, this); - - _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); } void Menu::onExit() diff --git a/extensions/GUI/CCControlExtension/CCControl.cpp b/extensions/GUI/CCControlExtension/CCControl.cpp index 2b97094acc..13d86e629c 100644 --- a/extensions/GUI/CCControlExtension/CCControl.cpp +++ b/extensions/GUI/CCControlExtension/CCControl.cpp @@ -80,6 +80,15 @@ bool Control::init() _dispatchTable = new Dictionary(); _dispatchTable->init(); + auto dispatcher = Director::getInstance()->getEventDispatcher(); + auto touchListener = EventListenerTouchOneByOne::create(); + touchListener->onTouchBegan = CC_CALLBACK_2(Control::onTouchBegan, this); + touchListener->onTouchMoved = CC_CALLBACK_2(Control::onTouchMoved, this); + touchListener->onTouchEnded = CC_CALLBACK_2(Control::onTouchEnded, this); + touchListener->onTouchCancelled = CC_CALLBACK_2(Control::onTouchCancelled, this); + + dispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); + return true; } else @@ -93,25 +102,6 @@ Control::~Control() CC_SAFE_RELEASE(_dispatchTable); } -void Control::onEnter() -{ - Layer::onEnter(); - - auto dispatcher = Director::getInstance()->getEventDispatcher(); - auto touchListener = EventListenerTouchOneByOne::create(); - touchListener->onTouchBegan = CC_CALLBACK_2(Control::onTouchBegan, this); - touchListener->onTouchMoved = CC_CALLBACK_2(Control::onTouchMoved, this); - touchListener->onTouchEnded = CC_CALLBACK_2(Control::onTouchEnded, this); - touchListener->onTouchCancelled = CC_CALLBACK_2(Control::onTouchCancelled, this); - - dispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); -} - -void Control::onExit() -{ - Layer::onExit(); -} - void Control::sendActionsForControlEvents(EventType controlEvents) { // For each control events diff --git a/extensions/GUI/CCControlExtension/CCControl.h b/extensions/GUI/CCControlExtension/CCControl.h index 0a9952803b..d8644a516f 100644 --- a/extensions/GUI/CCControlExtension/CCControl.h +++ b/extensions/GUI/CCControlExtension/CCControl.h @@ -182,17 +182,6 @@ public: // Overrides virtual bool isOpacityModifyRGB() const override; virtual void setOpacityModifyRGB(bool bOpacityModifyRGB) override; - /** - * @js NA - * @lua NA - */ - virtual void onEnter() override; - /** - * @js NA - * @lua NA - */ - virtual void onExit() override; -// virtual void registerWithTouchDispatcher() override; protected: /**