返回bool在cocos2dx的onTouchBegan()中意味着什么

时间:2017-08-17 07:02:30

标签: c++ events cocos2d-x

我发现以下代码仅适用于onTouchBegan()而不是onTouchMoved()onTouchEnded()

auto listener = EventListenerTouchOneByOne::create();

listener->onTouchBegan = [=](Touch *touch, Event *event) {
    CCLOG("on touch begain at (%f,%f)", touch->getLocation().x, touch->getLocation().y);
    return false; // this will make following two events couldn't be fired.
};
listener->onTouchMoved = [=](Touch *touch, Event *event) {
    CCLOG("on touch moved at (%f, %f)", touch->getLocation().x, touch->getLocation().y);
};

listener->onTouchEnded = [=](Touch *touch, Event * event) {
    CCLOG("on touch ended at (%f,%f)", touch->getLocation().x, touch->getLocation().y);
};

_eventDispatcher->addEventListenerWithFixedPriority(listener, 1);

发生这种情况的原因是分配给lambda函数的onTouchBegan在将其更改为falsetrueonTouchMoved后返回onTouchEnded按预期触发。

我在周围搜索过可以找到关于这个返回标志要做什么的任何解释?有人可以帮忙解释一下吗?

1 个答案:

答案 0 :(得分:2)

文档没有说明这一点,但programmers-guide确实:

// trigger when you push down
listener1->onTouchBegan = [](Touch* touch, Event* event)
{
    // your code
    return true; // if you are consuming it
};

因此,使用bool,您可以告诉系统是否要处理触摸事件。

另外:你的lambda中不需要任何捕获,所以最好使用[]而不是[=]