[英]CCLayer setTouchPriority has no effect
我创建了一个图层,其唯一目的是阻止(“吞咽”)触摸,并且可以打开和关闭此功能。 该类非常基础,如果收到触摸,它总是会吞下它:
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上。 任何帮助表示赞赏。
在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;
}
原来这是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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.