armnotstrong armnotstrong - 10 months ago 59
C++ Question

What does the return bool mean in onTouchBegan() of cocos2dx

I find following code will only work with

onTouchBegan()
and not
onTouchMoved()
and
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);


The reason why this happens is that the
onTouchBegan
assigned to a lambda function that returns
false
after changing this to
true
,
onTouchMoved
and
onTouchEnded
will be triggered as expected.

I searched around could find any explanation about what does this return flags intended to do? could some one help to explain that?

Answer Source

The documentation does not tell this, but the programmers-guide does:

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

So with the bool you can tell the system if you want to handle the touch event or not.

Also: you don't need any captures in your lambdas so better use [] instead of [=]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download