Cocos2d-x CCScale9Sprite 用法

1、创建方式有三种:

(1)、直接创建
auto blocks = Scale9Sprite::create("blocks9.png", Rect(0, 0, 96, 96), Rect(32, 32, 32, 32));

(2)、使用createWithSpriteFrameName创建
auto blocks_with_insets = Scale9Sprite::createWithSpriteFrameName("blocks9.png", Rect(32, 32, 32, 32));  //Rect为设置需要拉伸的区域

(1)、使用SpriteBatchNode的创建
auto batchNode = SpriteBatchNode::create("blocks9.png");
auto blocks = Scale9Sprite::create();
blocks->updateWithBatchNode(batchNode, Rect(0, 0, 96, 96), false, Rect(32, 32, 32, 32));  //第一个Rect为图片实际大小,第二个Rect同上

另外可设置Scale9Sprite的叠加透明色和透明度

blocks->setCascadeColorEnabled(true);
blocks->setCascadeOpacityEnabled(true);

2、举例:

STEP 1:使用PHOTOSHOP制作原始的小的图片(SIZE: 200 x100),如下图所示。


STEP 2: C++编码。

    Sprite* tmp = Sprite::create("scale.png");
    Size size = tmp->getContentSize();

  //创建原始的图像对应大小矩形区域。
    Rect fullRect = CCRect(0,0, size.width, size.height);

  //创建内部区域对应的矩形大小
  //注意:(11,11)这个点最关键,对应于上图中内部棕色矩形左上角的坐标
  //于是insetRect 即对应于内部的棕色矩形区域
  //实际应用中,我们要对这个内部区域进行放大或者缩小使用
    Rect insetRect = Rect(11, 11,size.width-11*2, size.height-11*2);

    Scale9Sprite* pSprite = Scale9Sprite::create("scale.png",fullRect, insetRect);

    pSprite->setContentSize(Size(400,200));
    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    this->addChild(pSprite, 0);

本例中,我们要创建大小为400X200大小的屏幕精灵。

STEP 3:上述代码运行结果如下。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值