为什么即使我在精灵外部触摸也会触发触摸事件?
Why is touch event triggered even when I touch outside the sprite?
我有一个 ui::ScrollView 包含许多精灵。
我已经创建了每个 sprite 并为每个 sprite 添加了一个触摸监听器,方法如下:
for(int i=0; i < 5; i++){
Sprite* foo = Sprite::createWithSpriteFrameName("foo");
myScrollView->addChild(foo);
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [this,somestring](Touch* touch, Event* event){
......some code
};
listener->onTouchMoved = [foo,this,somestring](Touch* touch, Event* event){
......some code
};
listener->onTouchEnded = [foo,this,somestring](Touch* touch, Event* event){
......some code
};
foo->getEventDispatcher->addEventListenerWithSceneGraphPriority(listener1,foo);
}
问题是,如果我点击屏幕上的任意位置,似乎会触发循环中创建的所有精灵的触摸事件。我创建侦听器的方式是否有不正确之处,或者它是否与 ui::ScrollView 中的触摸冲突有关?
我正在使用 v 3.10
因为 TouchListener 在 cocos2d-x 中就是这样工作的。除非有人吞下触摸事件,否则将调用所有触摸侦听器。您的代码将是:
auto touchSwallower = EventListenerTouchOneByOne::create();
touchSwallower ->setSwallowed(true);
touchSwallower->onTouchBegan = [](){ return true;};
getEventDispatcher->addEventListenerWithSceneGraphPriority(touchSwallower ,scrollview);
for(int i=0; i < 5; i++){
Sprite* foo = Sprite::createWithSpriteFrameName("foo");
myScrollView->addChild(foo);
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowed(true);
listener->onTouchBegan = [this,somestring](Touch* touch, Event* event){
......some code
Vec2 touchPos = myScrollView->convertTouchToNodeSpace(touch);
return foo->getBoundingBox()->containsPoint(touchPos);
};
listener->onTouchMoved = [foo,this,somestring](Touch* touch, Event* event){
......some code
};
listener->onTouchEnded = [foo,this,somestring](Touch* touch, Event* event){
......some code
};
foo->getEventDispatcher->addEventListenerWithSceneGraphPriority(listener1,foo);
}
cocos2dx 会将触摸事件发送给每个附加触摸事件的节点,除非有人吞下它。
但是如果你想要"node"默认判断内容中是否有touch-location,尝试使用"UIWidget"和"addTouchEventListener"。它会自己计算。
bool Widget::onTouchBegan(Touch *touch, Event *unusedEvent)
{
_hitted = false;
if (isVisible() && isEnabled() && isAncestorsEnabled() && isAncestorsVisible(this) )
{
_touchBeganPosition = touch->getLocation();
auto camera = Camera::getVisitingCamera();
if(hitTest(_touchBeganPosition, camera, nullptr))
{
if (isClippingParentContainsPoint(_touchBeganPosition)) {
_hittedByCamera = camera;
_hitted = true;
}
}
}
if (!_hitted)
{
return false;
}
setHighlighted(true);
/*
* Propagate touch events to its parents
*/
if (_propagateTouchEvents)
{
this->propagateTouchEvent(TouchEventType::BEGAN, this, touch);
}
pushDownEvent();
return true;
}
我有一个 ui::ScrollView 包含许多精灵。
我已经创建了每个 sprite 并为每个 sprite 添加了一个触摸监听器,方法如下:
for(int i=0; i < 5; i++){
Sprite* foo = Sprite::createWithSpriteFrameName("foo");
myScrollView->addChild(foo);
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [this,somestring](Touch* touch, Event* event){
......some code
};
listener->onTouchMoved = [foo,this,somestring](Touch* touch, Event* event){
......some code
};
listener->onTouchEnded = [foo,this,somestring](Touch* touch, Event* event){
......some code
};
foo->getEventDispatcher->addEventListenerWithSceneGraphPriority(listener1,foo);
}
问题是,如果我点击屏幕上的任意位置,似乎会触发循环中创建的所有精灵的触摸事件。我创建侦听器的方式是否有不正确之处,或者它是否与 ui::ScrollView 中的触摸冲突有关?
我正在使用 v 3.10
因为 TouchListener 在 cocos2d-x 中就是这样工作的。除非有人吞下触摸事件,否则将调用所有触摸侦听器。您的代码将是:
auto touchSwallower = EventListenerTouchOneByOne::create();
touchSwallower ->setSwallowed(true);
touchSwallower->onTouchBegan = [](){ return true;};
getEventDispatcher->addEventListenerWithSceneGraphPriority(touchSwallower ,scrollview);
for(int i=0; i < 5; i++){
Sprite* foo = Sprite::createWithSpriteFrameName("foo");
myScrollView->addChild(foo);
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowed(true);
listener->onTouchBegan = [this,somestring](Touch* touch, Event* event){
......some code
Vec2 touchPos = myScrollView->convertTouchToNodeSpace(touch);
return foo->getBoundingBox()->containsPoint(touchPos);
};
listener->onTouchMoved = [foo,this,somestring](Touch* touch, Event* event){
......some code
};
listener->onTouchEnded = [foo,this,somestring](Touch* touch, Event* event){
......some code
};
foo->getEventDispatcher->addEventListenerWithSceneGraphPriority(listener1,foo);
}
cocos2dx 会将触摸事件发送给每个附加触摸事件的节点,除非有人吞下它。
但是如果你想要"node"默认判断内容中是否有touch-location,尝试使用"UIWidget"和"addTouchEventListener"。它会自己计算。
bool Widget::onTouchBegan(Touch *touch, Event *unusedEvent)
{
_hitted = false;
if (isVisible() && isEnabled() && isAncestorsEnabled() && isAncestorsVisible(this) )
{
_touchBeganPosition = touch->getLocation();
auto camera = Camera::getVisitingCamera();
if(hitTest(_touchBeganPosition, camera, nullptr))
{
if (isClippingParentContainsPoint(_touchBeganPosition)) {
_hittedByCamera = camera;
_hitted = true;
}
}
}
if (!_hitted)
{
return false;
}
setHighlighted(true);
/*
* Propagate touch events to its parents
*/
if (_propagateTouchEvents)
{
this->propagateTouchEvent(TouchEventType::BEGAN, this, touch);
}
pushDownEvent();
return true;
}