节点对象外部的触摸也响应了触摸事件
Touches outside a Node object also responded touch event
在我的代码中,我添加了两个精灵,类型为 .png
,大小为 110*150。两个触摸侦听器被分配给每个精灵。不知何故,无论我触摸到哪里,即使我触摸到sprite之外,listerner2和listener都会响应,并且代码会打印listener2listener1
无论我触摸到哪里,我的代码有什么问题?
Scene* DemoScene::create()
{
auto demoScene = Scene::create();
if (demoScene == NULL) {
return NULL;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto demoSprite = Sprite::create("1.png");
demoSprite->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2));
demoScene->addChild(demoSprite);
auto demoSprite2 = Sprite::create("1.png");
demoSprite2->setPosition(Vec2(origin.x + visibleSize.width/3, origin.y + visibleSize.height/3));
demoScene->addChild(demoSprite2);
auto listener = EventListenerTouchOneByOne::create();
// listener->setSwallowTouches(true);
listener->onTouchBegan = [](Touch *touch, Event *event){
printf("listener1");
return true;
};
auto listener2 = EventListenerTouchOneByOne::create();
// listener2->setSwallowTouches(true);
listener2->onTouchBegan = [](Touch *touch, Event *event){
printf("listener2");
return true;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, demoSprite);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener2, demoSprite2);
return demoScene;
}
事件侦听器不会检查点击是否在 Sprite 内部。你应该手动控制它。如果 demoSprite 被触摸,在 onTouchBegan 方法上返回 true 将阻止将触摸传播到其他侦听器。 (如果 swallow touches 设置为 true)
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [](Touch *touch, Event *event){
// check if the touch is inside the target
Vec2 locationInNode = event->getCurrentTarget()->convertToNodeSpace(touch->getLocation());
Size s = event->getCurrentTarget()->getContentSize();
Rect rect = Rect(0,0, s.width, s.height);
if (rect.containsPoint(locationInNode)) {
printf("listener1");
return true;
}
return false;
};
auto listener2 = EventListenerTouchOneByOne::create();
listener2->setSwallowTouches(true);
listener2->onTouchBegan = [](Touch *touch, Event *event){
// check if the touch is inside the target
Vec2 locationInNode = event->getCurrentTarget()->convertToNodeSpace(touch->getLocation());
Size s = event->getCurrentTarget()->getContentSize();
Rect rect = Rect(0,0, s.width, s.height);
if (rect.containsPoint(locationInNode)) {
printf("listener2");
return true;
}
return false;
};
在我的代码中,我添加了两个精灵,类型为 .png
,大小为 110*150。两个触摸侦听器被分配给每个精灵。不知何故,无论我触摸到哪里,即使我触摸到sprite之外,listerner2和listener都会响应,并且代码会打印listener2listener1
无论我触摸到哪里,我的代码有什么问题?
Scene* DemoScene::create()
{
auto demoScene = Scene::create();
if (demoScene == NULL) {
return NULL;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto demoSprite = Sprite::create("1.png");
demoSprite->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2));
demoScene->addChild(demoSprite);
auto demoSprite2 = Sprite::create("1.png");
demoSprite2->setPosition(Vec2(origin.x + visibleSize.width/3, origin.y + visibleSize.height/3));
demoScene->addChild(demoSprite2);
auto listener = EventListenerTouchOneByOne::create();
// listener->setSwallowTouches(true);
listener->onTouchBegan = [](Touch *touch, Event *event){
printf("listener1");
return true;
};
auto listener2 = EventListenerTouchOneByOne::create();
// listener2->setSwallowTouches(true);
listener2->onTouchBegan = [](Touch *touch, Event *event){
printf("listener2");
return true;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, demoSprite);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener2, demoSprite2);
return demoScene;
}
事件侦听器不会检查点击是否在 Sprite 内部。你应该手动控制它。如果 demoSprite 被触摸,在 onTouchBegan 方法上返回 true 将阻止将触摸传播到其他侦听器。 (如果 swallow touches 设置为 true)
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [](Touch *touch, Event *event){
// check if the touch is inside the target
Vec2 locationInNode = event->getCurrentTarget()->convertToNodeSpace(touch->getLocation());
Size s = event->getCurrentTarget()->getContentSize();
Rect rect = Rect(0,0, s.width, s.height);
if (rect.containsPoint(locationInNode)) {
printf("listener1");
return true;
}
return false;
};
auto listener2 = EventListenerTouchOneByOne::create();
listener2->setSwallowTouches(true);
listener2->onTouchBegan = [](Touch *touch, Event *event){
// check if the touch is inside the target
Vec2 locationInNode = event->getCurrentTarget()->convertToNodeSpace(touch->getLocation());
Size s = event->getCurrentTarget()->getContentSize();
Rect rect = Rect(0,0, s.width, s.height);
if (rect.containsPoint(locationInNode)) {
printf("listener2");
return true;
}
return false;
};