简单实现监听layer(this)的3个触摸事件。
///触摸事件的处理 this->setTouchEnabled(true); auto dispatcher = Director::getInstance()->getEventDispatcher(); auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = [](Touch* touch,Event* event){ CCLOG("touch began"); return true;//一定要返回true ,否则后面的事件会监听不到 }; listener->onTouchMoved = [](Touch* touch,Event* event){ CCLOG("touch moved"); Vec2 v = touch->getLocation(); CCLOG("%f %f",v.x,v.y); }; listener->onTouchEnded = [=](Touch* touch,Event* event){ CCLOG("touch ended"); }; listener->setSwallowTouches(true);//不向下传递触摸 dispatcher->addEventListenerWithSceneGraPHPriority(listener,this);原文链接:https://www.f2er.com/cocos2dx/340725.html