Merge pull request #7552 from andyque/fixScale9Sprite

fix scale9Sprite addChild zorder issue
This commit is contained in:
minggo 2014-07-22 18:39:04 +08:00
commit 524d9cb8af
3 changed files with 148 additions and 21 deletions

View File

@ -131,8 +131,6 @@ bool Scale9Sprite::updateWithBatchNode(SpriteBatchNode* batchnode, const Rect& o
return false;
}
_scale9Image->removeAllChildrenWithCleanup(true);
_capInsets = capInsets;
_spriteFrameRotated = rotated;
@ -238,48 +236,48 @@ bool Scale9Sprite::updateWithBatchNode(SpriteBatchNode* batchnode, const Rect& o
// Centre
_centre = Sprite::createWithTexture(_scale9Image->getTexture(), centerbounds);
_centre->retain();
this->addChild(_centre, 0);
this->addChild(_centre);
// Top
_top = Sprite::createWithTexture(_scale9Image->getTexture(), centertopbounds);
_top->retain();
this->addChild(_top, 1);
this->addChild(_top);
// Bottom
_bottom = Sprite::createWithTexture(_scale9Image->getTexture(), centerbottombounds);
_bottom->retain();
this->addChild(_bottom, 1);
this->addChild(_bottom);
// Left
_left = Sprite::createWithTexture(_scale9Image->getTexture(), leftcenterbounds);
_left->retain();
this->addChild(_left, 1);
this->addChild(_left);
// Right
_right = Sprite::createWithTexture(_scale9Image->getTexture(), rightcenterbounds);
_right->retain();
this->addChild(_right, 1);
this->addChild(_right);
// Top left
_topLeft = Sprite::createWithTexture(_scale9Image->getTexture(), lefttopbounds);
_topLeft->retain();
this->addChild(_topLeft, 2);
this->addChild(_topLeft);
// Top right
_topRight = Sprite::createWithTexture(_scale9Image->getTexture(), righttopbounds);
_topRight->retain();
this->addChild(_topRight, 2);
this->addChild(_topRight);
// Bottom left
_bottomLeft = Sprite::createWithTexture(_scale9Image->getTexture(), leftbottombounds);
_bottomLeft->retain();
this->addChild(_bottomLeft, 2);
this->addChild(_bottomLeft);
// Bottom right
_bottomRight = Sprite::createWithTexture(_scale9Image->getTexture(), rightbottombounds);
_bottomRight->retain();
this->addChild(_bottomRight, 2);
this->addChild(_bottomRight);
} else {
// set up transformation of coordinates
// to handle the case where the sprite is stored rotated
@ -329,46 +327,46 @@ bool Scale9Sprite::updateWithBatchNode(SpriteBatchNode* batchnode, const Rect& o
// Top
_top = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedcentertopbounds, true);
_top->retain();
this->addChild(_top, 1);
this->addChild(_top);
// Bottom
_bottom = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedcenterbottombounds, true);
_bottom->retain();
this->addChild(_bottom, 1);
this->addChild(_bottom);
// Left
_left = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedleftcenterbounds, true);
_left->retain();
this->addChild(_left, 1);
this->addChild(_left);
// Right
_right = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedrightcenterbounds, true);
_right->retain();
this->addChild(_right, 1);
this->addChild(_right);
// Top left
_topLeft = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedlefttopbounds, true);
_topLeft->retain();
this->addChild(_topLeft, 2);
this->addChild(_topLeft);
// Top right
_topRight = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedrighttopbounds, true);
_topRight->retain();
this->addChild(_topRight, 2);
this->addChild(_topRight);
// Bottom left
_bottomLeft = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedleftbottombounds, true);
_bottomLeft->retain();
this->addChild(_bottomLeft, 2);
this->addChild(_bottomLeft);
// Bottom right
_bottomRight = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedrightbottombounds, true);
_bottomRight->retain();
this->addChild(_bottomRight, 2);
this->addChild(_bottomRight);
}
this->setContentSize(rect.size);
this->addChild(_scale9Image);
// this->addChild(_scale9Image);
if (_spritesGenerated)
{

View File

@ -46,7 +46,9 @@ static std::function<Layer*()> createFunctions[] = {
CL(S9_TexturePacker),
CL(S9FrameNameSpriteSheetRotatedInsetsScaled),
CL(S9FrameNameSpriteSheetRotatedSetCapInsetLater),
CL(S9CascadeOpacityAndColor)
CL(S9CascadeOpacityAndColor),
CL(S9ZOrder),
CL(S9Flip)
};
static int sceneIdx=-1;
@ -679,3 +681,104 @@ std::string S9CascadeOpacityAndColor::subtitle() const
{
return "when parent change color/opacity, Scale9Sprite should also change";
}
//
//// S9ZOrder
//
void S9ZOrder::onEnter()
{
S9SpriteTestDemo::onEnter();
auto winSize = Director::getInstance()->getWinSize();
float x = winSize.width / 2;
float y = 0 + (winSize.height / 2);
auto blocks_scaled_with_insets = Scale9Sprite::createWithSpriteFrameName("blocks9r.png");
blocks_scaled_with_insets->setPosition(Vec2(x, y));
this->addChild(blocks_scaled_with_insets);
Sprite *normalSprite = Sprite::createWithSpriteFrameName("blocks9r.png");
normalSprite->setColor(Color3B::RED);
blocks_scaled_with_insets->addChild(normalSprite);
auto topLabel = Label::createWithSystemFont("I Must be On the Top", "Arial", 15);
topLabel->setPosition(Vec2(20,20));
blocks_scaled_with_insets->addChild(topLabel);
auto bottomLabel = Label::createWithSystemFont("I Must be On the Bottom", "Arial", 15);
bottomLabel->setPosition(Vec2(80,80));
bottomLabel->setColor(Color3B::BLUE);
blocks_scaled_with_insets->addChild(bottomLabel,-1);
}
std::string S9ZOrder::title() const
{
return "Scale9Sprite ZOrder issue";
}
std::string S9ZOrder::subtitle() const
{
return "When adding nodes to Scale9Sprite, it should be added on top itself";
}
//
//// S9Flip
//
void S9Flip::onEnter()
{
S9SpriteTestDemo::onEnter();
auto winSize = Director::getInstance()->getWinSize();
float x = winSize.width / 2;
float y = 0 + (winSize.height / 2);
auto normalSprite = Scale9Sprite::createWithSpriteFrameName("blocks9r.png");
normalSprite->setPosition(Vec2(x, y ));
this->addChild(normalSprite);
auto normalLabel = Label::createWithSystemFont("Normal Sprite","Airal",10);
normalLabel->setPosition(normalSprite->getPosition() + Vec2(0, normalSprite->getContentSize().height/2 + 10));
this->addChild(normalLabel);
auto flipXSprite = Scale9Sprite::createWithSpriteFrameName("blocks9r.png");
flipXSprite->setPosition(Vec2(x - 120, y ));
this->addChild(flipXSprite);
flipXSprite->setScaleX(-1);
auto flipXLabel = Label::createWithSystemFont("Sprite FlipX","Airal",10);
flipXLabel->setPosition(flipXSprite->getPosition() + Vec2(0, flipXSprite->getContentSize().height/2 + 10));
this->addChild(flipXLabel);
auto flipYSprite = Scale9Sprite::createWithSpriteFrameName("blocks9r.png");
flipYSprite->setPosition(Vec2(x + 120, y));
this->addChild(flipYSprite);
flipYSprite->setScaleY(-1);
auto flipYLabel = Label::createWithSystemFont("Sprite FlipY","Airal",10);
flipYLabel->setPosition(flipYSprite->getPosition() + Vec2(0, flipYSprite->getContentSize().height/2 + 10));
this->addChild(flipYLabel);
}
std::string S9Flip::title() const
{
return "Scale9Sprite Flip issue";
}
std::string S9Flip::subtitle() const
{
return "When Flipped, the scale9Sprite should behavior like a normal node";
}

View File

@ -227,3 +227,29 @@ public:
virtual std::string title() const override;
virtual std::string subtitle() const override;
};
// Scale9Sprite ZOrder
class S9ZOrder : public S9SpriteTestDemo
{
public:
CREATE_FUNC(S9ZOrder);
virtual void onEnter() override;
virtual std::string title() const override;
virtual std::string subtitle() const override;
};
// Scale9Sprite Flip
class S9Flip : public S9SpriteTestDemo
{
public:
CREATE_FUNC(S9Flip);
virtual void onEnter() override;
virtual std::string title() const override;
virtual std::string subtitle() const override;
};