我发现以下代码仅适用于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
在将其更改为false
,true
和onTouchMoved
后返回onTouchEnded
按预期触发。
答案 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中不需要任何捕获,所以最好使用[]
而不是[=]