【问题标题】:cocos2d-x 3 How to make sprite swallow all touches only on itcocos2d-x 3 如何让精灵吞下所有的触摸只在它上面
【发布时间】:2014-06-25 22:17:12
【问题描述】:

我有一个main layer 应该获得触摸事件。但是在该层上有一个navigation bar,其中包含按钮和其他精灵,并且是cocos2d::Sprite 的子类。现在我需要所有对navigation bar 的触摸都不会被解释为对main layer 的触摸。按钮工作正常,但navigation bar sprite 将触摸传递给main layer。我这样做是为了防止传递事件:

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;
}

这会阻止传球,但会阻止 - 吞下所有触球。现在我无法将任何触摸传递给main layer,即使我不触摸navigation bar。我曾尝试使用setContentSize,但它没有帮助。解决办法在哪里?

【问题讨论】:

    标签: c++ cocos2d-x cocos2d-x-3.0


    【解决方案1】:

    答案在这里:http://www.cocos2d-x.org/wiki/How_To_Subclass_Sprite_And_Add_Event_Listeners

    我在这里更改了以下内容:

    • Vector2Vec2,
    • 删除了void touchEvent(cocos2d::Touch* touch, cocos2d::Vector2 _p); 中的第二个参数
    • MySprite::touchEvent(touch);touchEvent(touch);
    • cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 30);_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

    精灵应该检查触摸是否在它上面,而不是吞咽但在onTouchBeganlistener中返回true,否则false

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多