Hi,
I wanted to create a pop up dialog effect. And 1st thing about pop up is that it should block all touch events.
So i created a DecoyLayerForDialog that blocks all the touch events by returning true in ccTouchBegin and set the priority to kCCMenuHandlerPriority - 1 so that the touch event for menu in the dialog wont be blocked.
#include "DecoyLayerForDialog.h"
bool DecoyLayerForDialog::init(){
do{
CC_BREAK_IF( !CCLayer::init() );
cocos2d::CCSize winSize = cocos2d::CCDirector::sharedDirector()->getWinSize();
CCLayerColor *bg = CCLayerColor::layerWithColor( ccc4(0,0,0, 100), winSize.width, winSize.height );
this->addChild(bg);
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority - 1 , true);
return true;
}while(0);
return false;
}
bool DecoyLayerForDialog::ccTouchBegan(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent){
return true;
}
void DecoyLayerForDialog::ccTouchMoved(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent){
}
void DecoyLayerForDialog::ccTouchEnded(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent){
}
In the showDialog function I created a DecoyLayerForDialog .
Now I created anther layer called DialogLayer on which i’ll add all the content to be shown and CCMenu attached to it.
i added both layers according to z-index.
void DialogFactory::showDialog( _DIALOG_TYPE_ dialogType, SEL_MenuHandler selector ){
DecoyLayerForDialog *decoy = DecoyLayerForDialog::create();
CCLayer *dialog = CCLayer::create();
CCScene *currentScene = CCDirector::sharedDirector()->getRunningScene();
cocos2d::CCMenuItemFont *pOkButton = cocos2d::CCMenuItemFont::itemWithString("Exit", dialog, selector);
CCMenu *pMenu = CCMenu::menuWithItem(pOkButton);
dialog->addChild(pMenu, 100002);
currentScene->addChild(decoy, 100000);
currentScene->addChild(dialog, 100001);
}
But the CCMenu i attached to dialog layer is not working.
If i return false in DecoyLayerForDialog then menu in dialog works but the menu under the decoy layer also became tapable.
How to prioritize the touch delegates and make it swallow the event??
Here is the pictorial representation of the model I made.