CCLayer setTouchPriority无效

时间:2014-03-06 13:11:02

标签: android ios touch cocos2d-x cclayer

我创建了一个图层,其唯一目的是阻止(“吞下”)触摸,此功能可以打开和关闭。 该课程非常基础,如果它接受了触摸它总是吞下它:

bool BlockingLayer::init(){

    // Super init.
    if ( !CCLayer::init() )
    {
        return false;
    }

    setTouchEnabled(true);
    setTouchMode(kCCTouchesOneByOne);
    setTouchPriority(INT_MAX);

    return true;
}



bool BlockingLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
    CCLOG("BlockingLayer swallowed touch!");
    return true;
}

所以默认情况下它有一个非常糟糕的优先级,如果没有其他类声明它,它会接收到触摸。但是在我使用这个图层的场景中,我希望在发生某些事件时将其设置为不同的优先级:

bool MyScene::init(int unitNumber, CCString* path){
    // Super init.
    ...
    _blockingLayer = BlockingLayer::create();
    this->addChild(_blockingLayer);

    return true;
}

bool MyScene::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){
     _blockingLayer->setTouchPriority(INT_MIN);
     ...
}

现在该层应该具有最佳优先级,因此它应该吞下所有触摸。但事实并非如此,其行为并未改变。 我看到它的registerWithTouchDispatcher()被调用,m_nTouchPriority正确更改。但是图层的行为没有改变。

这是在Cocos2D-x 2.2上。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

在addTargetedDelegate()中将第三个参数设置为true

 bool BlockingLayer::init(){

    // Super init.
    if ( !CCLayer::init() )
    {
        return false;
    }

    setTouchEnabled(true);
    setTouchMode(kCCTouchesOneByOne);
    setTouchPriority(INT_MAX);

    return true;
}

void BlockingLayer::onEnter()
{
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, INT_MAX, true);  //<---- Param (target, touchPriority, isSwallowTouches )
    CCNode::onEnter();
}

void BlockingLayer::onExit()
{
     CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate( this );
     CCNode::onExit();
}


bool BlockingLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
    CCLOG("BlockingLayer swallowed touch!");
    return true;
}

答案 1 :(得分:0)

原来这是Cocos2d-x中的一个错误:

看到这个:

https://github.com/cocos2d/cocos2d-x/pull/5641

解决方案:

CCTouchDispatcher.cpp中的更改 这样:

void CCTouchDispatcher::addStandardDelegate(CCTouchDelegate *pDelegate, int nPriority)
{    
    CCTouchHandler *pHandler = CCStandardTouchHandler::handlerWithDelegate(pDelegate, nPriority);
    if (! m_bLocked)
    {
        forceAddHandler(pHandler, m_pStandardHandlers);
    }
    else
    {
        /* If pHandler is contained in m_pHandlersToRemove, if so remove it from     m_pHandlersToRemove and return.
         * Refer issue #752(cocos2d-x)
         */
         if (ccCArrayContainsValue(m_pHandlersToRemove, pDelegate))
         {
             ccCArrayRemoveValue(m_pHandlersToRemove, pDelegate);
             return;
          }

        m_pHandlersToAdd->addObject(pHandler);
        m_bToAdd = true;
    }
}

void CCTouchDispatcher::addTargetedDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallowsTouches)
{    
    CCTouchHandler *pHandler = CCTargetedTouchHandler::handlerWithDelegate(pDelegate, nPriority, bSwallowsTouches);
    if (! m_bLocked)
    {
        forceAddHandler(pHandler, m_pTargetedHandlers);
    }
    else
    {
        /* If pHandler is contained in m_pHandlersToRemove, if so remove it from m_pHandlersToRemove and return.
         * Refer issue #752(cocos2d-x)
         */
         if (ccCArrayContainsValue(m_pHandlersToRemove, pDelegate))
          {
              ccCArrayRemoveValue(m_pHandlersToRemove, pDelegate);
              return;
          }

        m_pHandlersToAdd->addObject(pHandler);
        m_bToAdd = true;
    }
}

到此:

void CCTouchDispatcher::addStandardDelegate(CCTouchDelegate *pDelegate, int nPriority)
{    
    CCTouchHandler *pHandler = CCStandardTouchHandler::handlerWithDelegate(pDelegate, nPriority);
    if (! m_bLocked)
    {
        forceAddHandler(pHandler, m_pStandardHandlers);
    }
    else
    {
        /* If pHandler is contained in m_pHandlersToRemove, if so remove it from m_pHandlersToRemove and return.
         * Refer issue #752(cocos2d-x)
         */
        if (ccCArrayContainsValue(m_pHandlersToRemove, pDelegate))
        {
            CCTouchHandler *pOldHandler = findHandler(pDelegate);
            if (pOldHandler && pOldHandler->getPriority() == nPriority)
            {
                ccCArrayRemoveValue(m_pHandlersToRemove, pDelegate);
                return;
            }
        }

        m_pHandlersToAdd->addObject(pHandler);
        m_bToAdd = true;
    }
}

void CCTouchDispatcher::addTargetedDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallowsTouches)
{    
    CCTouchHandler *pHandler = CCTargetedTouchHandler::handlerWithDelegate(pDelegate, nPriority, bSwallowsTouches);
    if (! m_bLocked)
    {
        forceAddHandler(pHandler, m_pTargetedHandlers);
    }
    else
    {
        /* If pHandler is contained in m_pHandlersToRemove, if so remove it from m_pHandlersToRemove and return.
         * Refer issue #752(cocos2d-x)
         */
        if (ccCArrayContainsValue(m_pHandlersToRemove, pDelegate))
        {
            CCTouchHandler *pOldHandler = findHandler(pDelegate);
            if (pOldHandler && pOldHandler->getPriority() == nPriority)
            {
                ccCArrayRemoveValue(m_pHandlersToRemove, pDelegate);
                return;
            }
        }

        m_pHandlersToAdd->addObject(pHandler);
        m_bToAdd = true;
    }
}