Cocos2dx3系のタッチイベントをラムダ式で書く

2014-07-20
blog
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [=](Touch* touch, Event* event) -> bool {
const Vec2& pos = touch->getLocation();
CCLOG("onTouchBegan: %.2f, %.2f", pos.x, pos.y);
return true;
};
listener->onTouchMoved = [=](Touch* touch, Event* event) -> void {
const Vec2& pos = touch->getLocation();
CCLOG("onTouchMoved: %.2f, %.2f", pos.x, pos.y);
};
listener->onTouchEnded = [=](Touch* touch, Event* event) -> void {
const Vec2& pos = touch->getLocation();
CCLOG("onTouchEnded: %.2f, %.2f", pos.x, pos.y);
};
listener->onTouchCancelled = [=](Touch* touch, Event* event) -> void {
const Vec2& pos = touch->getLocation();
CCLOG("onTouchCancelled: %.2f, %.2f", pos.x, pos.y);
};

auto dispatcher = Director::getInstance()->getEventDispatcher();
dispatcher->addEventListenerWithSceneGraphPriority(listener, this);