Modal CCLayer

Modal CCLayer
0.0 0


Hello guys!

What is the correct way to make CCLayer modal (ignore all touches, CCMenus, etc. if they have less zOrder)?

Thanks in advance!


ZOrder is not affecting to touch handling. To create modal layer you should subclass a cclayer and register is as a touch handler with lowest priority. And set swallowTouches to true.

void ModalLayer::onEnter()
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, INT_MIN + 1, true);

void ModalLayer::onExit()

bool ModalLayer::ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent)
    CCPoint touchLocation   = pTouch->locationInView();
    touchLocation           = CCDirector::sharedDirector()->convertToGL(touchLocation);
    touchLocation           = this->getParent()->convertToNodeSpace(touchLocation);
    if( !CCRect::CCRectContainsPoint(this->boundingBox(), touchLocation) )
        return false;
    return true;


Thank you for your reply! I have tried similar solution but it did not work for me. From your post I realised that my fault was with priority.


The way above is good, but not perfect.If I only want to make the layer modal on some layers, not all of the others, so how shall I do?


It is very simple. You should determine several levels of priority and set them dynamically to layer instance.