我有一个应该获取触摸事件的主层
。但在该层上有一个导航栏,其中包含按钮和其他 Sprite ,是 cocos2d::Sprite 的子类。现在我需要将导航栏
上的所有触摸都解释为对主层
的触摸。按钮工作正常,但导航栏
Sprite 将触摸传递到主层
。我这样做是为了防止传递事件:
auto touchListenerOneByOne = EventListenerTouchOneByOne::create();
touchListenerOneByOne->setSwallowTouches(true);
touchListenerOneByOne->onTouchBegan = CC_CALLBACK_2(NavigationBar::onBoardTouchBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListenerOneByOne, this);
bool NavigationBar::onBoardTouchBegan(Touch* touch, Event* event)
{
CCLOG("Navigation sprite is touched......!");
return true;
}
这可以防止传球,但它会阻止 - 吞噬所有接触。现在,即使我不触摸导航栏
,我也无法将任何触摸传递到主层
。我尝试使用 setContentSize 但没有帮助。解决办法在哪里?
最佳答案
答案在这里:http://www.cocos2d-x.org/wiki/How_To_Subclass_Sprite_And_Add_Event_Listeners
我在这里更改了以下内容:
Vector2
到Vec2
,- 删除了
void touchEvent(cocos2d::Touch* touch, cocos2d::Vector2 _p);
中的第二个参数 MySprite::touchEvent(touch);
到touchEvent(touch);
cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 30);
到_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
事实证明, Sprite 应该检查触摸是否在其上,然后吞下,但在 onTouchBegan
监听器中返回 true
,否则 false
.
关于c++ - cocos2d-x 3 如何让 Sprite 吞掉所有接触它的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24419529/