Android CCLayer setTouchPriority无效

Android CCLayer setTouchPriority无效,android,ios,touch,cocos2d-x,cclayer,Android,Ios,Touch,Cocos2d X,Cclayer,我创建了一个层,它的唯一目的是阻止(“吞咽”)接触,这个功能可以打开和关闭。 这门课非常基础,如果它受到触碰,它总是会吞下它: bool BlockingLayer::init(){ // Super init. if ( !CCLayer::init() ) { return false; } setTouchEnabled(true); setTouchMode(kCCTouchesOneByOne); setTou

我创建了一个层,它的唯一目的是阻止(“吞咽”)接触,这个功能可以打开和关闭。 这门课非常基础,如果它受到触碰,它总是会吞下它:

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_ntouch优先级已正确更改。但该层的行为没有改变

这在Cocos2D-x2.2上。非常感谢您的帮助。

在addTargetedElegate()中,将第三个参数设置为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;
}
bool BlockingLayer::init(){
//超级初始化。
如果(!CCLayer::init())
{
返回false;
}
setTouchEnabled(真);
设置触摸模式(kCCTouchesOneByOne);
setTouchPriority(INT_MAX);
返回true;
}
void BlockingLayer::onEnter()
{
CCDirector::sharedDirector()->getTouchDispatcher()->AddTargetedElegate(this,INT_MAX,true);//getTouchDispatcher()->RemoveDeleteGate(this);
CCNode::onExit();
}
bool BlockingLayer::CCTouch开始(CCTouch*pTouch,CCEvent*pEvent)
{
CCLOG(“BlockingLayer吞下触摸!”);
返回true;
}

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

见此:

解决方案:

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))
        {
            CCTouchHandler *pOldHandler = findHandler(pDelegate);
            if (pOldHandler && pOldHandler->getPriority() == nPriority)
            {
                ccCArrayRemoveValue(m_pHandlersToRemove, pDelegate);
                return;
            }
        }

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


我发现这个错误修复导致了这个新的错误:最后一条评论正好显示了这个问题。如果你在任何层上添加
TargetDelegate
,那么你必须删除它才能正常工作。您可以在
onExit()
中删除,也可以在需要的任何位置手动调用。所有这些都会在启用触摸的CCLayers上自动发生。将自动调用他们的registerWithTouchDispatcher方法,该方法调用所提到的行。:)但我在非CCLayer类中使用您的方法。:)
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;
    }
}