Hi, I am new for cocos2d-x, and now work on cocos2d-x 3.0.
today I found when I use ControlButton, if I push a scene then pop it, the first scene which used ControlButton will called twice.
and then I read the code of Control, found in 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);
I don’t found any code to remove it. and every time onEnter will do this.
so I changed code like:
void Control::onEnter()
{
Layer::onEnter();
// we don't need every time to add listener
if(!_touchListener)
{
auto dispatcher = Director::getInstance()->getEventDispatcher();
_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()
{
if(_touchListener)
{
auto dispatcher = Director::getInstance()->getEventDispatcher();
dispatcher->removeEventListener(_touchListener);
_touchListener = NULL;
}
Layer::onExit();
}
I will push it to github, but it’s my first time use github, need some times to do it.
CCControl.h.zip (3.4 KB)
CCControl.cpp.zip (3.2 KB)
CCMenu.cpp.zip (3.9 KB)
CCMenu.h.zip (1.9 KB)