zoukankan      html  css  js  c++  java
  • 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:上述代码运行结果如下。

  • 相关阅读:
    [bzoj2038] [2009国家集训队]小Z的袜子
    浅谈莫队
    [bzoj2754] [SCOI2012]喵星球上的点名
    [bzoj3676] [APIO2014]回文串
    [bzoj5472] 数列
    [bzoj5457] 城市
    [bzoj1023] [SHOI2008]cactus仙人掌图
    [bzoj2125] 最短路
    [bzoj5473] 仙人掌
    读《深入理解Elasticsearch》点滴-查询评分
  • 原文地址:https://www.cnblogs.com/DswCnblog/p/3570875.html
Copyright © 2011-2022 走看看