程序中使用cc.ScaleTo:create(t, scale_x, scale_y, scale_z),如果scale_z设置成0,这是不影响UI的具体显示,但是在对UI进行addTouchEventListener做点击监听时,会不响应点击事件。
后来跟了源码发现,cocos2d再对widget组件做事件监听时有调用:hitTest方法
if(hitTest(_touchBeganPosition, camera, nullptr))
{
if (isClippingParentContainsPoint(_touchBeganPosition)) {
_hittedByCamera = camera;
_hitted = true;
}
}
hitTest方法主要用于判断触摸点是否在节点范围内。
最后调用到isScreenPointInRect方法:
bool Widget::hitTest(const Vec2 &pt, const Camera* camera, Vec3 *p) const
{
Rect rect;
rect.size = getContentSize();
return isScreenPointInRect(pt, camera, getWorldToNodeTransform(), rect, p);
}
该方法在CCNode中定义是:
this is a helper function, checks a GL screen point is in content rectangle space.
This function convert GL screen point to near and far planes as points Pn and Pf,
then calculate the intersect point P which the line PnPf intersect with content rectangle.
所以它的点击有涉及到摄像机远近点的裁剪问题,我们上面把Z轴的scale写成0,导致widget控件无法响应点击事件。