CCLayer中的setTouchEnabled(true)会开启多点触摸。
如果使用CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);
只会开启一个点的触摸。
当HelloWorld::ccTouchBegan返回为false的时候,TouchDispatcher将会相应下一个优先级的触摸代理。
boolHelloWorld::ccTouchBegan(CCTouch*pTouch,CCEvent*pEvent){
returnfalse;
}
,如果在HelloWorld中没有设置setTouchEnabled(true),将不会响应其他触摸,因为程序中没有其他的触摸代理了。
当HelloWorld::ccTouchBegan返回为true的时候,在界面上滑动就会调用HelloWorld::ccTouchMoved(CCTouch*pTouch,CCEvent*pEvent);
在CCLayer中,默认是没有开启触摸的,如果需要相应多点触摸事件,就必须setTouchEnabled(true)。
下面看看CCLayer中的setTouchEnabled,如果设置为true,就会以0为优先级来注册触摸监听代理,优先级的值越小,优先级越高,触摸响应是按照优先级来排列响应的。
voidCCLayer::setTouchEnabled(boolenabled)
{
if(m_bIsTouchEnabled!=enabled)
{
m_bIsTouchEnabled=enabled;
if(m_bIsRunning)
{
if(enabled)
{
this->registerWithTouchDispatcher();
}
else
{
//haveproblems?
CCDirector*pDirector=CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->removeDelegate(this);
}
}
}
}
voidCCLayer::registerWithTouchDispatcher()
{
CCDirector*pDirector=CCDirector::sharedDirector();
if(m_pScriptHandlerEntry)
{
if(m_pScriptHandlerEntry->isMultiTouches())
{
pDirector->getTouchDispatcher()->addStandardDelegate(this,0);
LUALOG("[LUA]Addmulti-toucheseventhandler:%d",m_pScriptHandlerEntry->getHandler());
}
else
{
pDirector->getTouchDispatcher()->addTargetedDelegate(this,
m_pScriptHandlerEntry->getPriority(),
m_pScriptHandlerEntry->getSwallowsTouches());
LUALOG("[LUA]Addtoucheventhandler:%d",m_pScriptHandlerEntry->getHandler());
}
return;
}
pDirector->getTouchDispatcher()->addStandardDelegate(this,0);
}