【问题标题】:Why is touch event triggered even when I touch outside the sprite?为什么即使我触摸精灵外部也会触发触摸事件?
【发布时间】:2016-03-21 08:10:43
【问题描述】:

我有一个包含许多精灵的 ui::ScrollView。

我已经创建了每个精灵,并通过执行以下操作为每个精灵添加了一个触摸侦听器:

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

【问题讨论】:

    标签: cocos2d-x cocos2d-x-3.0 cocos2d-x-3.x


    【解决方案1】:

    因为 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);
    }
    

    【讨论】:

      【解决方案2】:

      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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-27
        • 1970-01-01
        • 1970-01-01
        • 2021-12-26
        • 1970-01-01
        相关资源
        最近更新 更多