初始化时隐藏UI仍可触发TouchEvent

新建了一个Hello world项目
在init()加载了三个带按钮的UI,分别为uiStart,uiPlay和uiPause,然后对后两个UI进行
setTouchEnabled(false);和setVisible(false);
跟踪的时候也可以发现初始化后uiPlay和uiPause的m_bTouchEnabled 已为 false。
但启动后被隐藏的UI上的按钮事件仍会触发。
而同样被放在按钮事件中的setTouchEnabled(false);和setVisible(false);重新隐藏之后不会出现隐藏ui按钮被触发的情况。

求教如何正确初始化?

你好 ,目前隐藏和触摸是同级控制,所以当您不需要点击时请使用setTouchEnabled(false);

我在创建的时候setTouchEnabled(false);之后还是会触发touch事件。

UILayer* uiPlay = UILayer::create();  
uiPlay->addWidget(dynamic_cast<Layout*>(GUIReader::shareReader()->widgetFromJsonFile("UI_Play/UI_Play.json"))); 
this->addChild(uiPlay, 21, eUI_Play);  

UIButton* BtnPause =dynamic_cast<UIButton*>(uiPlay->getWidgetByName( "BtnPause"));
BtnPause->addTouchEventListener(this, toucheventselector(HelloWorld::btnPauseTouchEvent));
UIButton* BtnLeft =dynamic_cast<UIButton*>(uiPlay->getWidgetByName( "BtnLeft"));
BtnLeft->addTouchEventListener(this, toucheventselector(HelloWorld::btnLeftTouchEvent));
UIButton* BtnRight =dynamic_cast<UIButton*>(uiPlay->getWidgetByName( "BtnRight"));
BtnRight->addTouchEventListener(this, toucheventselector(HelloWorld::btnRightTouchEvent));
uiPlay->setVisible(false);

UILayer* uiPause = UILayer::create();  
uiPause->addWidget(dynamic_cast<Layout*>(GUIReader::shareReader()->widgetFromJsonFile("UI_Pause/UI_Pause.json"))); 
this->addChild(uiPause, 20, eUI_Pause);  

UIButton* BtnContinue =dynamic_cast<UIButton*>(uiPause->getWidgetByName( "BtnContinue"));
BtnContinue->addTouchEventListener(this, toucheventselector(HelloWorld::btnContinueTouchEvent));
UIButton* BtnRestart =dynamic_cast<UIButton*>(uiPause->getWidgetByName( "BtnRestart"));
BtnRestart->addTouchEventListener(this, toucheventselector(HelloWorld::btnRestartTouchEvent));
UIButton* BtnStop =dynamic_cast<UIButton*>(uiPause->getWidgetByName( "BtnStop"));
BtnStop->addTouchEventListener(this, toucheventselector(HelloWorld::btnStopTouchEvent));
uiPause->setVisible(false);

UILayer* uiStart = UILayer::create();  
uiStart->addWidget(dynamic_cast<Layout*>(GUIReader::shareReader()->widgetFromJsonFile("UI_Start/UI_Start.json"))); 
this->addChild(uiStart, 42, eUI_Start);  
UIButton* BtnStart = dynamic_cast<UIButton*>( uiStart->getWidgetByName( "BtnStart" ));
BtnStart->addTouchEventListener(this, toucheventselector(HelloWorld::btnStartTouchEvent));
uiStart->setVisible(true);

上面这段是在uiInit()里的,然后:

///////////////////////////// 
// 3. add your codes below... 
this->uiInit(); 
dynamic_cast<UILayer*>(this->getChildByTag(eUI_Play))->setTouchEnabled(false); 
dynamic_cast<UILayer*>(this->getChildByTag(eUI_Pause))->setTouchEnabled(false); 

还是不行

而在按钮的touch事件里触发的setTouchEnable(false);是有效的

void HelloWorld::btnPauseTouchEvent(CCObject pSender, TouchEventType type ){
switch (type)
{
case TOUCH_EVENT_ENDED:{
//隐藏uiPlay
UILayer
ul_u =dynamic_cast<UILayer*>(this->getChildByTag(eUI_Play));
ul_u->setTouchEnabled(false);
//显示uiPause
UILayer* ul_v =dynamic_cast<UILayer*>(this->getChildByTag(eUI_Pause));
ul_v->setTouchEnabled(true);
ul_v->setVisible(true);
}
break;
default:break;
}
}

这个没有办法解决了吗?

算了,那我就挨个控件setTouchEnabled(false); 吧

跟踪了一下,发现在init对UI进行setTouchEnable(false)之后会被重新set成true。
不知道VS2010怎么设置数据断点,暂时无法具体定位被异常修改该的地方。

要setEnable(false); 才行的