今天寫了 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); 這句說明了一切。