Seems it is a bug

My own class inherit from CCLayer, but somehow it rigster standardTouchDelegate failed at first.

CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this,0);

void CCTouchDispatcher::addStandardDelegate(CCTouchDelegate *pDelegate, int nPriority)
{
    CCTouchHandler *pHandler = CCStandardTouchHandler::handlerWithDelegate(pDelegate, nPriority);
    if (! m_bLocked)
    {
        forceAddHandler(pHandler, m_pStandardHandlers);
    }
    else
    {
        m_pHandlersToAdd->addObject(pHandler);
        m_bToAdd = true;
    }
}

the m_bLocked is true, so it add to m_pHandlersToAdd.

then

if (m_bToAdd)
    {
        m_bToAdd = false;
        CCMutableArray::CCMutableArrayIterator iter;
         CCTouchHandler *pHandler;
        for (iter = m_pHandlersToAdd->begin(); iter != m_pHandlersToAdd->end(); ++iter)
        {
            pHandler = *iter;
            if (! pHandler)
            {
                break;
            }

            if (pHandler->getDelegate()->getTouchDelegateType() & ccTouchDelegateTargetedBit)
            {               
                forceAddHandler(pHandler, m_pTargetedHandlers);
            }
            else
            {
                forceAddHandler(pHandler, m_pStandardHandlers);
            }
        }

        m_pHandlersToAdd->removeAllObjects();   
    }

but,

CCLayer::CCLayer()
:m_bIsTouchEnabled(false)
,m_bIsAccelerometerEnabled(false)
,m_bIsKeypadEnabled(false)
,m_bIsMouseEnabled(false)
,m_bIsKeyboardEnabled(false)
{
    m_eTouchDelegateType = ccTouchDeletateAllBit;
    m_tAnchorPoint = ccp(0.5f, 0.5f);
    m_bIsRelativeAnchorPoint = false;
}

So, you can see following judgement is always true for CCLayer, in another word standardTouch changes to TargetTouch.

if (pHandler->getDelegate()->getTouchDelegateType() & ccTouchDelegateTargetedBit)
{               
    forceAddHandler(pHandler, m_pTargetedHandlers);
}

so, I got the error :

Assertion failed: (false), function ccTouchBegan, cocos2dx/platform/CCLayer_mobile.cpp, line 298.

In theory, your situation is

class YourLayer :public CCLayer

You should call void CCLayer::setIsTouchEnabled(bool enabled) just like

bool YourLayer::init()
{
     // do something

     // touch enable
     this->setIsTouchEnabled(true);
}

instead of invoking CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this,0); directly.
Otherwise, the flag CCLayer::m_bIsTouchEnabled will stands in false, it may couse some uncertain logic flow.

Hope this can help you.

I used the way you said, I called setIsTouchEnabled(ture), I will never directly called addStandardDelegate. I know it will be called by CCLayer::OnEnter().

Maybe you should read carefully about my post…

Sorry, I apologize for my carelessness.
I just traced the source and understand what you mean.
It’s a bug here, not easy to find out. The condition of (m_tLock == true) isn’t covered by the existing unit tests. It only appears when you try to add a new layer in touches event.
In objc version, “isKindOfClass” method used. And we had to judge the enum type in c++ version.

OK, I have found the way to fix it elegantly. Give me 20 minutes to fix bug and write a unit test…

Bug #467 is fixed at https://github.com/cocos2d/cocos2d-x/commit/f9e3a92aa92aae1e0078e722eb8957889b74b25d
I write a simple test code to reproduce & test, TouchLayer.zip
Thanks for your bug report & trace!

Also thanks for your fixing, the problem stuck me several days until I start to trace the engine code,
my simple fixing method is delaying about 0.1f time, before add layer, just let the touch dispatch finish.

aha, I found the same bug is commit 1 month ago: Bug #371.
Thanks god, they are all fixed now.

I am facing this same issue , i am using cocos2d-x 2.0 …

" Error is assertion failed: (0), function forceAddHandler file /Users/…/libs/cocos2dx/touch_dispatcher/CCTouchDispatcher.cpp, line 121 "

i am using the sneaky button joystick in game…

I am not getting how to overcome… :frowning:
Please help me if someone solved this issue ….

Thanks in advance.
Mangesh