armnotstrong armnotstrong - 1 year ago 80
C++ Question

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

I find following code will only work with

and not

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
assigned to a lambda function that returns
after changing this to
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