cocos2dx 3.x 触摸事件
单点触摸:
bool onTouchBegan(cocos2d::Touch *pTouch, cocos2d::Event *pEvent);
void onTouchMoved(cocos2d::Touch *pTouch, cocos2d::Event *pEvent);
void onTouchEnded(cocos2d::Touch *pTouch, cocos2d::Event *pEvent);
多点触摸:
void onTouchesBegan(const std::vector<cocos2d::Touch *>& touches, cocos2d::Event *unused_event);
void onTouchesMoved(const std::vector<cocos2d::Touch *>& touches, cocos2d::Event *unused_event);
void onTouchesEnded(const std::vector<cocos2d::Touch *>& touches, cocos2d::Event *unused_event);
触摸注册:
EventDispatcher *dispatcher = Director::getInstance()->getEventDispatcher();
EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(MyLayer::onTouchBegan,this);
listener->onTouchMoved = CC_CALLBACK_2(MyLayer::onTouchMoved,this);
listener->onTouchEnded = CC_CALLBACK_2(MyLayer::onTouchEnded,this);
dispatcher->addEventListenerWithSceneGraphPriority(listener,this);
listen->setSwallowTouches(true);
setSwallowTouches用于设置是否吞没事件,也就是当某个触摸事件回调的时,截断该事件,让它不能继续传递给其他人。同时2个层,上层写了之后,下面的层获取不到点击事件.