最近做窗口的适配缩放时发现窗口界面的子节点page(class Page : public CCLayer)的可点击区域没有随着父节点的缩放而改变,所以需要设定一个m_scale的变量来储存父节点的scale从而改变可点击区域
代码块
bool Page::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
if (!isVisible())
return false;
CCPoint pt = convertToWorldSpace(CCPointZero);
CCPoint ptTouch = pTouch->getLocation();
CCSize contentSize = getContentSize();
// touch在page范围内
if (ptTouch.x < pt.x || ptTouch.x > pt.x + contentSize.width*m_Scale || ptTouch.y < pt.y || ptTouch.y > pt.y + contentSize.height*m_Scale)
{
return false;
}
clearDeadTouchDelegate();
for (list<TouchDelegate>::iterator it = m_touchNodes.begin(); it != m_touchNodes.end(); ++it)
{
if(eTouchDead == it->status)
continue;
eTouchStatus e = it->delegate->ccTouchBegan(pTouch, pEvent) ? eTouchDown : eTouchNone;
if(it->status != eTouchDead)
it->status = e;
}
return true;
}