使用Scale9Sprite须要引入以下的头文件
#include "extensions/cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT;
一、S9BatchNodeBasic
bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); float x = visibleSize.width / 2; float y = visibleSize.height / 2; auto batchNode = SpriteBatchNode::create("blocks9.png"); auto blocks = Scale9Sprite::create(); log("------created"); blocks->updateWithBatchNode(batchNode, Rect(0, 0, 96, 96), false, Rect(0, 0, 96, 96)); blocks->setPosition(Point(x, y)); this->addChild(blocks); return true; }
效果:
二、S9FrameNameSpriteSheet
bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("blocks9ss.plist"); float x = visibleSize.width / 2; float y = visibleSize.height / 2; auto blocks = Scale9Sprite::createWithSpriteFrameName("blocks9.png"); blocks->setPosition(Point(x, y)); this->addChild(blocks); return true; }
效果:
三、S9BatchNodeScaledNoInsets 设置精灵的大小
bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("blocks9ss.plist"); float x = visibleSize.width / 2; float y = visibleSize.height / 2; auto batchNode_scaled = SpriteBatchNode::create("blocks9.png"); auto block_scaled = Scale9Sprite::create(); block_scaled->updateWithBatchNode(batchNode_scaled, Rect(0, 0, 96, 96), false, Rect(0, 0, 96, 96)); block_scaled->setPosition(Point(x, y)); block_scaled->setContentSize(Size(96 * 4, 96 * 2)); this->addChild(block_scaled); return true; }
效果:
四、S9FrameNameSpriteSheetRotatedScaledNoInsets
auto block_scaled = Scale9Sprite::createWithSpriteFrameName("blocks9.png"); block_scaled->setPosition(Point(x, y)); block_scaled->setContentSize(Size(96 * 4, 96 * 2)); this->addChild(block_scaled);
效果:
五、S9FrameNameSpriteSheetRotatedScaledNoInsets
auto block_scaled = Scale9Sprite::createWithSpriteFrameName("blocks9r.png"); block_scaled->setPosition(Point(x, y)); block_scaled->setContentSize(Size(96 * 4, 96 * 2)); this->addChild(block_scaled);
效果:
S9FrameNameSpriteSheetInsets
auto batchNode_scaled_with_insets = SpriteBatchNode::create("blocks9.png"); auto blocks_scaled_with_insets = Scale9Sprite::create(); blocks_scaled_with_insets->updateWithBatchNode(batchNode_scaled_with_insets, Rect(0, 0, 96, 96), false, Rect(32, 32, 32, 32)); blocks_scaled_with_insets->setContentSize(Size(96 * 4.5, 96 * 2.5)); blocks_scaled_with_insets->setPosition(Point(x, y)); this->addChild(blocks_scaled_with_insets);
S9FrameNameSpriteSheetInsetsScaled
Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("blocks9ss.plist"); float x = visibleSize.width / 2; float y = visibleSize.height / 2; auto blocks_scaled_with_insets = Scale9Sprite::createWithSpriteFrameName("blocks9.png",Rect(32, 32, 32, 32)); blocks_scaled_with_insets->setContentSize(Size(96 * 4.5, 96 * 2.5)); blocks_scaled_with_insets->setPosition(Point(x, y)); this->addChild(blocks_scaled_with_insets);
S9_TexturePacker
Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui.plist"); float x = visibleSize.width / 4; float y = visibleSize.height / 2; auto s = Scale9Sprite::createWithSpriteFrameName("button_normal.png"); s->setPosition(Point(x, y)); log("... setPosition"); s->setContentSize(Size(14 * 16, 10 * 16)); log("... setContentSize"); this->addChild(s); auto blocks_scaled_with_insets = Scale9Sprite::createWithSpriteFrameName("button_actived.png"); x = visibleSize.width * 3/4; blocks_scaled_with_insets->setContentSize(Size(14 * 16, 10 * 16)); blocks_scaled_with_insets->setPosition(Point(x , y)); this->addChild(blocks_scaled_with_insets);
Scale9FrameNameSpriteSheetRotatedSetCapInsetLater
Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("blocks9ss.plist"); float x = visibleSize.width / 2; float y = visibleSize.height / 2; auto blocks_scaled_with_insets = Scale9Sprite::createWithSpriteFrameName("blocks9r.png"); blocks_scaled_with_insets->setInsetLeft(32); blocks_scaled_with_insets->setInsetRight(32); blocks_scaled_with_insets->setPreferredSize(Size(32 * 5.5f, 32 * 4)); blocks_scaled_with_insets->setPosition(Point(x , y)); this->addChild(blocks_scaled_with_insets);
S9CascadeOpacityAndColor
Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("blocks9ss.plist"); float x = visibleSize.width / 2; float y = visibleSize.height / 2; auto rgba = Layer::create(); rgba->setCascadeColorEnabled(true); rgba->setCascadeOpacityEnabled(true); this->addChild(rgba); auto blocks_scaled_with_insets = Scale9Sprite::createWithSpriteFrameName("blocks9r.png"); blocks_scaled_with_insets->setPosition(Point(x , y)); rgba->addChild(blocks_scaled_with_insets); auto actions = Sequence::create(FadeIn::create(1),TintTo::create(1, 0, 255, 0),TintTo::create(1, 255, 255, 255),FadeOut::create(1), NULL); auto repeat = RepeatForever::create(actions); rgba->runAction(repeat);