最近做視窗的适配縮放時發現視窗界面的子節點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;
}