prevent touch propagation to other layers

prevent touch propagation to other layers
0.0 0


How do I stop the propagation so that only the top most layer handle the touch event?

in cocos2d-iphone, someone suggested

  • (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    return kEventHandled;

but I couldn’t find kEventHandled in cocos2d-x

probably kEventHandled is obsoleted?



Not sure if this is a proper way to swallow touches.

void UpdateLayer::onEnter()
    CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true);

void UpdateLayer::onExit()

bool UpdateLayer::ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent)
    CCLOG("Prevent Touch Propagation in Update Layer");
    return true;


In cocos2d system, standard touch delegates cannot swallow the touch message. If you’re not using multi-touch, targeted delegate can be an option.

    virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent); return false;};
    // optional

    virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
    virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
    virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}

The message will be cut if you return false in ccTouchBegan. The difference between standard touch delegate and targeted touch delegate is here[]=touch&s[]=delegate

The topic you referred from cocos2d-iphone, it just a source hack from original code. You can also hack it in cocos2d-x source in standard touch delegate.