版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhangge3663/article/details/83239799
1.addEventListenerWidthSceneGraphPriority函数,这个函数的两个参数作用如下:
- EventListener *listener:事件监听对象,当触摸事件发生时通过它来回调;
- Node *node:绑定的对象,当node对象被释放时,监听事件的注册也会被取消,同时,有多个触摸事件发生时(比如几个按钮叠加在一起),会根据node层次优先回调(越在上面的对象越先回调);
addEventListenerWithFixedPriority,也是用于注册监听事件,但这个函数需要手动指定触摸事件回调的优先级,并且需要手动取消监听事件。一帮情况下,我们使用addEventListenerWidthSceneGraphPriority就可以了。
bool HelloWorld::init() {
if (!Layer::init()) { return false; }
Size visibleSize = Director::getInstance()->getVisibleSize();
/* 创建两个精灵,相互有重叠的部分 */
Sprite *sp1 = Sprite::create("sprite1.png");
sp1->setPosition(Point(visibleSize.width * 0.5f, visibleSize.height * 0.5f));
this->addChild(sp1);
Sprite *sp2 = Sprite::create("sprite2.png");
sp2->setPosition(Point(visibleSize.width * 0.5f, visibleSize.height * 0.5f));
this->addChild(sp2);
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [](Touch *touch, Event *event) {
/* 注册监听事件时绑定了一个Node对象,在这里就可以取出这个对象 */
auto target = static_cast<Sprite *>(event->getCurrentTarget());
Point pos = Director::getInstance()->convertToGL(touch->getLocationInView());
/* 判断单击的坐标是否在精灵的范围内 */
if (target->getBoundingBox().containsPoint(pos)) {
/* 设置精灵的透明度为100 */
target->setOpacity(100);
return true;
}
return false;
};
listener->onTouchEnded = [](Touch *touch, Event *event) {
/* 恢复精灵的透明度 */
auto target = static_cast<Sprite *>(event->getCurrentTarget());
target->setOpacity(255);
};
/* 注册监听事件,绑定精灵1 */
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, sp1);
/*注册监听事件,绑定精灵2,这里要注意,listener对象复制了一份*/
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), sp2);
return true;
}
分步骤来讲解:
a) 创建两个精灵,让这两个精灵刚好有部分位置是重叠的;
b) 创建EventListenerTouchOneByOne监听事件;
c) 在onTouchBegan函数里获取事件绑定的精灵对象,判断单击的坐标是否在精灵的范围内,是的话,则修改精灵的透明度为100;
d) 调用addEventListenerWithSceneGraphPriority函数分别添加两个精灵的监听事件;
通常我们要求在单击重叠部位的时候,只有上面的按钮能获得响应。要实现这个效果,很简单,我们给listener调用一个函数即可:
.listener->setSwallowTouches(true);
setSwallowTouches函数用于设置是否吞没事件。
2.多点触控
bool HelloWorld::init() {
if (!Layer::init()) { return false; }
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = [](const std::vector<Touch *>& touches, Event *event) {};
listener->onTouchesMoved = [](const std::vector<Touch *>& touches, Event *event) {};
listener->onTouchesEnded = [](const std::vector<Touch *>& touches, Event *event) {};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
bool HelloWorld::init() {
if (!Layer::init()) { return false; }
Label *logText1 = Label::create("", "Arial", 24);
logText1->setPosition(Point(400, 280));
this->addChild(logText1, 1, 1);
Label *logText2 = Label::create("", "Arial", 24);
logText3->setPosition(Point(400, 100));
this->addChild(logText3, 1, 3);
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = [&](const std::vector<Touch *>& touches, Event *event) {
auto logText = (Label *)this->getChildByTag(1);
int num = touches.size();
logText->setString(Value(num).asString() + " Touches:");
};
listener->onTouchesMoved = [&](const std::vector<Touch *>& touches, Event *event) {
auto logText = (Label *)this->getChildByTag(2);
int num = touches.size();
std::string text = Value(num).asString() + " Touches:";
for (auto &touch : touches) {
auto location = touch->getLocation();
text += "[touchID" + Value(touch->getID()).asString() + "],";
}
logText->setString(text);
};
listener->onTouchesEnded = [&](const std::vector<Touch *>& touches, Event *event) {
auto logText = (Label *)this->getChildByTag(3);
int num = touches.size();
logText->setString(Value(num).asString() + " Touches:");
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}