cocos2d-x 3.x中Layer层触摸事件
·
简单实现监听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);
更多推荐
已为社区贡献3条内容
所有评论(0)