首页 > 代码库 > cocos2d-x getBoundingBox getContentSize getTextureRect 解释

cocos2d-x getBoundingBox getContentSize getTextureRect 解释

这是cocos2d-x代码看注释的解释 翻译的 不到之处谅解 后面在总结
getBoundingBox  // 返回一个AABB(axis-aligned bounding-box)在其父母的坐标系统。Returns an AABB (axis-aligned bounding-box) in its parent‘s coordinate system.
getContentSize  //节点容量大小
getTextureRect //返回精灵矩形的点

例子1
{
图片 Icon-48.png  这种图片 48 × 48 pixels 标准icon中得一个 
    Sprite *pSpBox=Sprite::create("Icon-48.png");
    pSpBox->setPosition(Vec2(m_pWinSize.width*0.5, m_pWinSize.height*0.5));
    pSpBox->setScale(0.2f);
    addChild(pSpBox);
    {
        CCLOG("A");
        Rect rect=pSpBox->getBoundingBox();
        Size size=pSpBox->getContentSize();
        Rect sizea=pSpBox->getTextureRect();
        CCLOG("BoundingBox(%f,%f,%f,%f)",rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
        CCLOG("ContentSize(%f,%f)",size.width,size.height);
        CCLOG("TextureRect(%f,%f,%f,%f)",sizea.origin.x,sizea.origin.y,sizea.size.width,sizea.size.height);
    }
    pSpBox->setScale(0.5f);
    {
        CCLOG("B");
        Rect rect=pSpBox->getBoundingBox();
        Size size=pSpBox->getContentSize();
        Rect sizea=pSpBox->getTextureRect();
        CCLOG("BoundingBox(%f,%f,%f,%f)",rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
        CCLOG("ContentSize(%f,%f)",size.width,size.height);
        CCLOG("TextureRect(%f,%f,%f,%f)",sizea.origin.x,sizea.origin.y,sizea.size.width,sizea.size.height);
    }

cocos2d: A
cocos2d: BoundingBox(475.200012,315.200012,9.600006,9.600006)
cocos2d: ContentSize(48.000000,48.000000)
cocos2d: TextureRect(0.000000,0.000000,48.000000,48.000000)
cocos2d: B
cocos2d: BoundingBox(468.000000,308.000000,24.000000,24.000000)
cocos2d: ContentSize(48.000000,48.000000)
cocos2d: TextureRect(0.000000,0.000000,48.000000,48.000000)
}


例子2
{

图片 Icon-48.png  这种图片 48 × 48 pixels 标准icon中得一个 

    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("haohaoxuexi.plist");//把现有的icon图片整到一个大图中测试
    Sprite *pSpBox=Sprite::createWithSpriteFrameName("Icon-48.png");//create("Icon-48.png");
    pSpBox->setPosition(Vec2(m_pWinSize.width*0.5, m_pWinSize.height*0.5));
    pSpBox->setScale(0.2f);
    addChild(pSpBox);
    
    {
        CCLOG("A");
        Rect rect=pSpBox->getBoundingBox();
        Size size=pSpBox->getContentSize();
        Rect sizea=pSpBox->getTextureRect();
        CCLOG("BoundingBox(%f,%f,%f,%f)",rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
        CCLOG("ContentSize(%f,%f)",size.width,size.height);
        CCLOG("TextureRect(%f,%f,%f,%f)",sizea.origin.x,sizea.origin.y,sizea.size.width,sizea.size.height);
    }
    pSpBox->setScale(0.5f);
    {
        CCLOG("B");
        Rect rect=pSpBox->getBoundingBox();
        Size size=pSpBox->getContentSize();
        Rect sizea=pSpBox->getTextureRect();
        CCLOG("BoundingBox(%f,%f,%f,%f)",rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
        CCLOG("ContentSize(%f,%f)",size.width,size.height);
        CCLOG("TextureRect(%f,%f,%f,%f)",sizea.origin.x,sizea.origin.y,sizea.size.width,sizea.size.height);
    }
//输出结果
cocos2d: A
cocos2d: BoundingBox(475.200012,315.200012,9.600006,9.600006)
cocos2d: ContentSize(48.000000,48.000000)
cocos2d: TextureRect(447.000000,1616.000000,48.000000,48.000000)
cocos2d: B
cocos2d: BoundingBox(468.000000,308.000000,24.000000,24.000000)
cocos2d: ContentSize(48.000000,48.000000)
cocos2d: TextureRect(447.000000,1616.000000,48.000000,48.000000)

//"haohaoxuexi.plist" 中图片Icon-48.png 数据做参考
 <key>Icon-48.png</key>
    <dict>
        <key>frame</key>
        <string>{{447,1616},{48,48}}</string>
        <key>offset</key>
        <string>{0,0}</string>
        <key>rotated</key>
        <false/>
        <key>sourceColorRect</key>
        <string>{{0,0},{48,48}}</string>
        <key>sourceSize</key>
        <string>{48,48}</string>
    </dict>

}

应该都知道什么意思了,还是总结一下吧

getBoundingBox  中得 Size.width .height  显示图片真实大小 (考虑缩放和不缩放)
getContentSize  纹理图片大小
getTextureRect  当前的纹理在总纹理的位置 (不考虑 缩放不缩放)


图片有缩放 就用 getBoundingBox ,不考虑缩放用 getContentSize
看需求用哪个?


getBoundingBox 中得 origin.x .y 本人也不知道什么意思,打印出来没有看懂。前做这点解释,知道后在更新

cocos2d-x getBoundingBox getContentSize getTextureRect 解释