[Cocos2d-x]节点的尺寸大小

作为一个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

转载于:https://www.cnblogs.com/hellenism/p/3472639.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值