cocos2dx tableview添加button必现的bug

  我所在的项目中,在tableview中的cell中添加button,发现当button超过tableview的可见范围时,点击button仍然会触发触摸事件。

想了下,tableview、cell与button是有父子关系的,出现这种情况可能是他们的关系在做范围判断时断掉。

  于是挖挖挖,在UIWidget.cpp的isClippingParentContainsPoint(const Vec2 &pt)函数中发现问题,先贴代码:

_affectByClipping = false;
Widget* parent = getWidgetParent();
Widget* clippingParent = nullptr;
while (parent)
{
  Layout* layoutParent = dynamic_cast<Layout*>(parent);
  if (layoutParent)
  {
    if (layoutParent->isClippingEnabled())
    {
      _affectByClipping = true;
      clippingParent = layoutParent;
      break;
    }
  }
  parent = parent->getWidgetParent();
}

  这个函数是判断是否在父控件范围内的函数,然而,在parent = getWidgetParent();中,是通过强制转换成Widget类型来拿到parent的,

而button的parent是tableviewcell,tableviewcell继承自node类,而Widget类是node的子类,相当于做了一次父类转子类的强制转换,这当然

是不可以,所以到了这一层就断了,从而判断不到button是否在tableview的范围内。

  那怎么办呢,很简单,把getWidgetParent()换成getParent()就可以了。

  把Widget* parent = getWidgetParent();换成Node* parent = getParent();

  parent = parent->getWidgetParent();换成parent = parent->getParent();

  目的达成。

 

  

posted @ 2016-05-24 19:37  泫鱼  阅读(2009)  评论(1编辑  收藏  举报