2017-08-17 99 views
-2

的onTouchBegan()的意思是我发现下面的代码将只与onTouchBegan()工作,而不是onTouchMoved()onTouchEnded()是什么返回布尔在cocos2dx

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

之所以出现这种情况是,onTouchBegan分配给lambda函数在将其更改为trueonTouchMovedonTouchEnded将按预期触发后返回false

我周围搜索可以找到关于这个返回标志打算做什么的任何解释?有人能帮助解释吗?

回答

2

文档没有讲这个,但programmers-guide做:

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

所以与布尔你可以告诉系统,如果你要处理的触摸事件与否。

另外:你不需要在你的Lambda表达式任何捕捉所以最好使用[]代替[=]

+0

您好,感谢您的解释,我已阅读本文档,我还以为是只为'onTouchBegan'行动,从来没有想过它会影响同一个听众中的另外两个消费者。我知道'[]'和'[=]'的用法,但是感谢提醒,我认为它更像是一个表演的东西,对吧?谢谢 – armnotstrong

+0

不仅表现而且用法!取决于你想要对lamda中的数据做什么。如果不使用任何捕获的项目,我不确定如果编译器优化了捕获。 –