cocos2d3.0 Scale9Sprite

使用Scale9Sprite需要引入下面的头文件

  1. #include "extensions/cocos-ext.h"  
  2.   
  3.   
  4. USING_NS_CC;  
  5. USING_NS_CC_EXT;  

一、S9BatchNodeBasic

  1. bool HelloWorld::init()  
  2. {  
  3.     //  
  4.     // 1. super init first  
  5.     if ( !Layer::init() )  
  6.     {  
  7.         return false;  
  8.     }  
  9.       
  10.     Size visibleSize = Director::getInstance()->getVisibleSize();  
  11.     Point origin = Director::getInstance()->getVisibleOrigin();  
  12.   
  13.       
  14.     float x = visibleSize.width / 2;  
  15.     float y = visibleSize.height / 2;  
  16.       
  17.     auto batchNode = SpriteBatchNode::create("blocks9.png");  
  18.       
  19.     auto blocks = Scale9Sprite::create();  
  20.     log("------created");  
  21.       
  22.     blocks->updateWithBatchNode(batchNode, Rect(0, 0, 96, 96), false, Rect(0, 0, 96, 96));  
  23.       
  24.     blocks->setPosition(Point(x, y));  
  25.       
  26.     this->addChild(blocks);  
  27.     return true;  
  28. }  

效果:



二、 S9FrameNameSpriteSheet

  1. bool HelloWorld::init()  
  2. {  
  3.     //  
  4.     // 1. super init first  
  5.     if ( !Layer::init() )  
  6.     {  
  7.         return false;  
  8.     }  
  9.       
  10.     Size visibleSize = Director::getInstance()->getVisibleSize();  
  11.     Point origin = Director::getInstance()->getVisibleOrigin();  
  12.   
  13.      SpriteFrameCache::getInstance()->addSpriteFramesWithFile("blocks9ss.plist");  
  14.     float x = visibleSize.width / 2;  
  15.     float y = visibleSize.height / 2;  
  16.       
  17.     auto blocks = Scale9Sprite::createWithSpriteFrameName("blocks9.png");  
  18.   
  19.     blocks->setPosition(Point(x, y));  
  20.       
  21.     this->addChild(blocks);  
  22.       
  23.       
  24.     return true;  
  25. }  

效果:



三、 S9BatchNodeScaledNoInsets  设置精灵的大小
  1. bool HelloWorld::init()  
  2. {  
  3.     //  
  4.     // 1. super init first  
  5.     if ( !Layer::init() )  
  6.     {  
  7.         return false;  
  8.     }  
  9.       
  10.     Size visibleSize = Director::getInstance()->getVisibleSize();  
  11.     Point origin = Director::getInstance()->getVisibleOrigin();  
  12.   
  13.      SpriteFrameCache::getInstance()->addSpriteFramesWithFile("blocks9ss.plist");  
  14.     float x = visibleSize.width / 2;  
  15.     float y = visibleSize.height / 2;  
  16.       
  17.     auto batchNode_scaled = SpriteBatchNode::create("blocks9.png");  
  18.       
  19.     auto block_scaled = Scale9Sprite::create();  
  20.       
  21.     block_scaled->updateWithBatchNode(batchNode_scaled, Rect(0, 0, 96, 96), false, Rect(0, 0, 96, 96));  
  22.       
  23.     block_scaled->setPosition(Point(x, y));  
  24.       
  25.     block_scaled->setContentSize(Size(96 * 4, 96 * 2));  
  26.       
  27.     this->addChild(block_scaled);  
  28.       
  29.       
  30.       
  31.     return true;  
  32. }  

效果:



四、 S9FrameNameSpriteSheetRotatedScaledNoInsets

  1. auto block_scaled = Scale9Sprite::createWithSpriteFrameName("blocks9.png");  
  2.   
  3.     
  4.   block_scaled->setPosition(Point(x, y));  
  5.     
  6.   block_scaled->setContentSize(Size(96 * 4, 96 * 2));  
  7.     
  8.   this->addChild(block_scaled);  
  9.     

效果:



五、 S9FrameNameSpriteSheetRotatedScaledNoInsets
  1. auto block_scaled = Scale9Sprite::createWithSpriteFrameName("blocks9r.png");  
  2.   
  3.   
  4. block_scaled->setPosition(Point(x, y));  
  5.   
  6. block_scaled->setContentSize(Size(96 * 4, 96 * 2));  
  7.   
  8. this->addChild(block_scaled);  

效果:



S9FrameNameSpriteSheetInsets

  1. auto batchNode_scaled_with_insets = SpriteBatchNode::create("blocks9.png");  
  2.   
  3. auto blocks_scaled_with_insets = Scale9Sprite::create();  
  4.   
  5. blocks_scaled_with_insets->updateWithBatchNode(batchNode_scaled_with_insets, Rect(0, 0, 96, 96), false, Rect(32, 32, 32, 32));  
  6.   
  7. blocks_scaled_with_insets->setContentSize(Size(96 * 4.5, 96 * 2.5));  
  8.   
  9. blocks_scaled_with_insets->setPosition(Point(x, y));  
  10.   
  11. this->addChild(blocks_scaled_with_insets);  




S9FrameNameSpriteSheetInsetsScaled

  1. Size visibleSize = Director::getInstance()->getVisibleSize();  
  2. Point origin = Director::getInstance()->getVisibleOrigin();  
  3.   
  4.  SpriteFrameCache::getInstance()->addSpriteFramesWithFile("blocks9ss.plist");  
  5. float x = visibleSize.width / 2;  
  6. float y = visibleSize.height / 2;  
  7.   
  8.   
  9. auto blocks_scaled_with_insets = Scale9Sprite::createWithSpriteFrameName("blocks9.png",Rect(32, 32, 32, 32));  
  10.   
  11.   
  12.   
  13. blocks_scaled_with_insets->setContentSize(Size(96 * 4.5, 96 * 2.5));  
  14.   
  15. blocks_scaled_with_insets->setPosition(Point(x, y));  
  16.   
  17. this->addChild(blocks_scaled_with_insets);  






S9_TexturePacker

  1. Size visibleSize = Director::getInstance()->getVisibleSize();  
  2. Point origin = Director::getInstance()->getVisibleOrigin();  
  3.   
  4.  SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui.plist");  
  5. float x = visibleSize.width / 4;  
  6. float y = visibleSize.height / 2;  
  7.   
  8.   
  9. auto s = Scale9Sprite::createWithSpriteFrameName("button_normal.png");  
  10.   
  11. s->setPosition(Point(x, y));  
  12. log("... setPosition");  
  13.   
  14. s->setContentSize(Size(14 * 16, 10 * 16));  
  15. log("... setContentSize");  
  16.   
  17. this->addChild(s);  
  18.   
  19.   
  20.   
  21. auto blocks_scaled_with_insets = Scale9Sprite::createWithSpriteFrameName("button_actived.png");  
  22.   
  23.   
  24.  x = visibleSize.width * 3/4;  
  25. blocks_scaled_with_insets->setContentSize(Size(14 * 16, 10 * 16));  
  26.   
  27. blocks_scaled_with_insets->setPosition(Point(x , y));  
  28.   
  29. this->addChild(blocks_scaled_with_insets);  





Scale9FrameNameSpriteSheetRotatedSetCapInsetLater

  1. Size visibleSize = Director::getInstance()->getVisibleSize();  
  2.   Point origin = Director::getInstance()->getVisibleOrigin();  
  3.   
  4.    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("blocks9ss.plist");  
  5.   float x = visibleSize.width / 2;  
  6.   float y = visibleSize.height / 2;  
  7.     
  8.     
  9.     
  10.   
  11.     
  12.   auto blocks_scaled_with_insets = Scale9Sprite::createWithSpriteFrameName("blocks9r.png");  
  13.   blocks_scaled_with_insets->setInsetLeft(32);  
  14.   blocks_scaled_with_insets->setInsetRight(32);  
  15.   blocks_scaled_with_insets->setPreferredSize(Size(32 * 5.5f, 32 * 4));  
  16.     
  17.   blocks_scaled_with_insets->setPosition(Point(x , y));  
  18.     
  19.   this->addChild(blocks_scaled_with_insets);  







S9CascadeOpacityAndColor

  1. Size visibleSize = Director::getInstance()->getVisibleSize();  
  2. Point origin = Director::getInstance()->getVisibleOrigin();  
  3.   
  4.  SpriteFrameCache::getInstance()->addSpriteFramesWithFile("blocks9ss.plist");  
  5. float x = visibleSize.width / 2;  
  6. float y = visibleSize.height / 2;  
  7.   
  8. auto rgba = Layer::create();  
  9. rgba->setCascadeColorEnabled(true);  
  10. rgba->setCascadeOpacityEnabled(true);  
  11. this->addChild(rgba);  
  12.   
  13.   
  14.   
  15.   
  16. auto blocks_scaled_with_insets = Scale9Sprite::createWithSpriteFrameName("blocks9r.png");  
  17.   
  18.   
  19. blocks_scaled_with_insets->setPosition(Point(x , y));  
  20.   
  21. rgba->addChild(blocks_scaled_with_insets);  
  22.   
  23.   
  24.   
  25. auto actions = Sequence::create(FadeIn::create(1),TintTo::create(1, 0, 255, 0),TintTo::create(1, 255, 255, 255),FadeOut::create(1), NULL);  
  26. auto repeat = RepeatForever::create(actions);  
  27.   
  28. rgba->runAction(repeat);  






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值