prevent touch propagation to other layers

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?

related:
http://www.cocos2d-iphone.org/forum/topic/2632

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

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

void UpdateLayer::onExit()
{
    CCTouchDispatcher::sharedDispatcher()->removeDelegate(this);
    CCLayer::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 http://www.cocos2d-iphone.org/wiki/doku.php/tips:touchdelegates?s[]=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.