作為一個CCNode,本身沒有大小而言,但是AddChild之后,便有了尺寸的概念。
Cocos2d-x中對於一個節點的尺寸可以通過以下三個方法獲取:
CCSprite:
getContentSize();
獲取精靈的邏輯尺寸。此值不受縮放變換影響。即setScale()方法不影響此值
boundingBox().size;
獲取精靈的邊框尺寸。此值受到縮放變換影響。
getTexture()->getContentSizeInPixels();
獲取精靈的紋理的像素尺寸。
像素點和邏輯點關系:邏輯點大小 = 像素大小/contentScale
Ex:
CCSize sizeByContentSize = pBackground->getContentSize(); CCSize sizeByboundingBox = pBackground->boundingBox().size; CCSize sizeByPoints = pBackground->getTexture()->getContentSizeInPixels(); CCLOG("sizeByContentSize:width:%f height:%f",sizeByContentSize.width,sizeByContentSize.height); CCLOG("sizeByboundingBox:width:%f height:%f",sizeByboundingBox.width,sizeByboundingBox.height); CCLOG("sizeByPoints:width:%f height:%f",sizeByPoints.width,sizeByPoints.height); pBackground->setScale(2); CCLOG("轉變后:"); sizeByContentSize = pBackground->getContentSize(); sizeByboundingBox = pBackground->boundingBox().size; sizeByPoints =pBackground->getTexture()->getContentSizeInPixels(); CCLOG("sizeByContentSize:width:%f height:%f",sizeByContentSize.width,sizeByContentSize.height); CCLOG("sizeByboundingBox:width:%f height:%f",sizeByboundingBox.width,sizeByboundingBox.height); CCLOG("sizeByPoints:width:%f height:%f",sizeByPoints.width,sizeByPoints.height);
輸出結果:
Cocos2d: sizeByContentSize:width:114.000000 height:114.000000
Cocos2d: sizeByboundingBox:width:114.000000 height:114.000000
Cocos2d: sizeByPoints:width:114.000000 height:114.000000
Cocos2d: 轉變后:
Cocos2d: sizeByContentSize:width:114.000000 height:114.000000
Cocos2d: sizeByboundingBox:width:228.000000 height:228.000000
Cocos2d: sizeByPoints:width:114.000000 height:114.000000