单点触摸:
auto label = LabelTTF::create("Label","Courier",30);
label->setPosition(visibleSize / 2);
this->addChild(label);
auto listener = EventListenerTouchOneByOne::create(); /*创建事件侦听*/
listener->onTouchBegan = [](Touch *touch,Event* event){ /*开始点击触发回调函数*/
if (event->getCurrentTarget()->getBoundingBox().containsPoint( /*触摸点是否在目标范围*/ touch->getLocation())){ CCLOG("OK"); } return false; /*事件是否传递 false不传递,true传递*/ }; /*在场景中注册事件*/ Director::getInstance()->getEventDispatcher()->
addEventListenerWithSceneGraPHPriority(listener,label);
多点触摸:
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = [](std::vector<Touch*> touchs,Event* event){
CCLOG("Touchs Began!");
};
listener->onTouchesMoved = [](std::vector<Touch*> touchs,Event* event){
CCLOG("Touchs Move: %d",touchs.size());
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,this);