-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函数在将其更改为true
,onTouchMoved
和onTouchEnded
将按预期触发后返回false
。
我周围搜索可以找到关于这个返回标志打算做什么的任何解释?有人能帮助解释吗?
您好,感谢您的解释,我已阅读本文档,我还以为是只为'onTouchBegan'行动,从来没有想过它会影响同一个听众中的另外两个消费者。我知道'[]'和'[=]'的用法,但是感谢提醒,我认为它更像是一个表演的东西,对吧?谢谢 – armnotstrong
不仅表现而且用法!取决于你想要对lamda中的数据做什么。如果不使用任何捕获的项目,我不确定如果编译器优化了捕获。 –