cocos2d-x CCScale9Sprite实例

    // 只是简单获取一下图形大小

         CCSprite* tmp = CCSprite::create("extensions/background.png");

        CCSize size = tmp->getContentSize();

        CCRect fullRect = CCRectMake(0,0, size.width, size.height);

        CCRect insetRect = CCRectMake(3,3,size.width-6, size.height-6);

        CCLog("wh:%f,%f", size.width, size.height);

        tmp->release();

 // 调用CCScale9Sprite

        CCScale9Sprite* backGround = CCScale9Sprite::create("extensions/background.png", fullRect, insetRect ); 

        backGround->setPreferredSize(CCSizeMake(300, 30));

        backGround->setPosition(ccp(10, 230));

        backGround->setAnchorPoint(CCPointZero);

        addChild(backGround);

加入图片是圆角图片,圆角半径为3,图片大小为16x16,则rectInsets最好设置为(3,3,10,10); 其中,3,3是中间区域的起点,10,10是中间区域的宽高,10是16-2*3的结果.

注意:在测试中发现,如果想要setPreferredSize()或者setContentSize()在收缩图片时生效,则只能用

 CCScale9Sprite::create("extensions/background.png", fullRect, insetRect ) 这个构造函数,用
 CCScale9Sprite::create("extensions/background.png") 或者
CCScale9Sprite::create(insetRect,"extensions/background.png", )
这两个create函数构造出来的CCScale9Sprite都无法按照理想中的收缩,但拉伸是没问题的!

BUG原因:

CCScale9Sprite* CCScale9Sprite::create(CCRect capInsets, const char* file)的实现有误,应该将该函数中的

if ( pReturn && pReturn->initWithFile(file,capInsets) )
{
pReturn->autorelease();
return pReturn;
}

改为

if ( pReturn && pReturn->initWithFile(capInsets, file) )
{
pReturn->autorelease();
return pReturn;
}

即可

 
  
 
  

 

 

你可能感兴趣的:(cocos2d-x)