cocos2d-x事件EventListenerTouchOneByOne没反应

今天写了 cocos2d-x事件EventListenerTouchOneByOne,发现死活没反应,原代码复制到新工程没问题啊, 后来发现cocostudio用的基础容器(ccui.Layout:create())默认是可交互性的,导出lua发现setTouchEnabled(true); 设置可触摸是true,这就明了拉,是Layout(继承Widget)挡住了事件下发了,按照cocos2d-x事件的原理看了C++源码,发现:

void Widget::setTouchEnabled(bool enable)
{
if (enable == _touchEnabled)
{
return;
}
_touchEnabled = enable;
if (_touchEnabled)
{
_touchListener = EventListenerTouchOneByOne::create();
CC_SAFE_RETAIN(_touchListener);
_touchListener->setSwallowTouches(true);
_touchListener->onTouchBegan = CC_CALLBACK_2(Widget::onTouchBegan, this);
_touchListener->onTouchMoved = CC_CALLBACK_2(Widget::onTouchMoved, this);
_touchListener->onTouchEnded = CC_CALLBACK_2(Widget::onTouchEnded, this);
_touchListener->onTouchCancelled = CC_CALLBACK_2(Widget::onTouchCancelled, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(_touchListener, this);
}
else
{
_eventDispatcher->removeEventListener(_touchListener);
CC_SAFE_RELEASE_NULL(_touchListener);
}
}
_touchListener->setSwallowTouches(true); 这句说明了一切。
posted @ 2016-12-31 15:55  黔王  阅读(2093)  评论(0编辑  收藏  举报