CCScale9Sprite

Hi,
do anyone have an example how to use CCScale9Sprite with setCapInsets? Every time i call the setCapInset function the sprite gets invisible.

THX


The origion for CCScle9Sprite is upper left not lower left.


Beside there is a bug in the following function:

TCCScale9Sprite* TCCScale9Sprite::create(CCRect capInsets, const char* file)
{
TCCScale9Sprite* pReturn = new TCCScale9Sprite();
if ( pReturn && pReturn~~>initWithFile )
{
pReturn~~>autorelease();
return pReturn;
}
CC_SAFE_DELETE(pReturn);
return NULL;
}

It have to be if ( pReturn && pReturn->initWithFile(capInsets, file) )

Did you ever find a solution to this problem? I’m trying to ninepatch scale an image and getting the same result

No … i made a copy of the file and modified the source where it needed to be.

Hi,

I have the same issue: the sprite is not rendered as soon as I set the cap insets (calling setInsetLeft/Top… or setCapInsets)…

@ CCScale9Sprite*s = CCScale9Sprite::createWithSpriteFrameName(“messagebox”);
s~~>setPreferredSize);
s~~>setInsetLeft(15.f);
s~~>setInsetRight;
s~~>setInsetTop(25.0f);
s~~>setInsetBottom;
s~~>setAnchorPoint(ccp(.5f, .5f));
addChild(s);@

Is this the right way to create a CCScale9Sprite?
I couldn’t find any examples in the tests/samples which use setInsetLeft/right/top etc…

Any help?

You need to setContentSize. In my understanding the CCScale9Sprite scales around the content, if there is none it just doesn’t show anything.

I’ve tested with setContentSize & setPreferredSize but still the same issue…

Daniel Brain is right … you need to set the content size. you have to add the caps to the content size. that means if you want to have a inner size of 100 pixel you have to set content site width to 100 + left margin + right margin.

Hi den ranx, did you came with a solution of this?

Or can anyone modify his code to see an example of a working CCScale9Sprite from CCSpriteBatchNode?

Thanks

Hi,

Sorry for the late feedback but here’s how I simply managed to create and resize a CCScale9Sprite.

I create a symmetric image (see screenshot) and load it using the following code. The sprite is automatically split into a 3×3 grid of equal blocks and we can use setContentSize to resize the sprite (only the center block of the sprite will be scaled)

    CCScale9Sprite*sp = CCScale9Sprite::createWithSpriteFrameName("spriteFrameName");
    sp->setContentSize( CCSizeMake(200, 150) );

So am I the only one who can’t create a CCScale9Sprite? I don’t know what else to do.

Here’s my code:

CCScale9Sprite* buttonBG = CCScale9Sprite::createWithSpriteFrameName("menuButtonTop.png");
buttonBG->setContentSize(CCSizeMake(winSize.width-20, 67));
menuElementsBatchNode->addChild(buttonBG);

That way there will be an assertion failure when adding the child to the BatchNode.

CCScale9Sprite* buttonBG = CCScale9Sprite::createWithSpriteFrameName("menuButtonTop.png", CCRectMake(5,5,8,8));
buttonBG->setContentSize(CCSizeMake(winSize.width-20, 67));
menuElementsBatchNode->addChild(buttonBG);

That way there will be an assertion failure when splitting the CCScale9Sprite because the coordinates are calculated the wrong way.

Any tip?

Hi Danadn,

I use CCScale9Sprite::createWithSpriteFrameName because my texture is already loaded and available in the sprite frame cache (CCSpriteFrameCache).

To load a sprite from file use CCScale9Sprite::create(“spriteFileName.png”).

Hope it helps.

Hi den ranx,

I know that, in fact this is my whole code:

    CCSpriteBatchNode* menuElementsBatchNode = CCSpriteBatchNode::create(pvrPath.append("/MenuScene/menuElements.pvr.ccz").c_str());
    this->addChild(menuElementsBatchNode, 0);
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(plistPath.append("/MenuScene/menuElements.plist").c_str());

    CCScale9Sprite* buttonBG = CCScale9Sprite::createWithSpriteFrameName("menuButtonTop.png");
    buttonBG->setContentSize(CCSizeMake(winSize.width-20, 67));
    menuElementsBatchNode->addChild(buttonBG);

Am I doing something wrong? Since I don’t have any sample I can’t compare and also this works for normal CCSprite’s

CCScale9Sprite* buttonBG = CCScale9Sprite::createWithSpriteFrameName("menuButtonTop.png");
buttonBG->setContentSize(CCSizeMake(winSize.width-20, 67));
menuElementsBatchNode->addChild(buttonBG);

What is the assertion failure message?

Maybe the assertion failed because you’re trying to add CCScale9Sprite which is not a subclass of CCSprite to a CCSpriteBatchNode.

EDIT:

@den: I’ve just seen your answer, so it was that I can’t add CCScale9Sprites to CCSpriteBatchNode. I don’t know if that’s a good practice since for CCSprites the correct way is too add them to the batchNode.

However CCScale9Sprite has a bug with custom insets, it calculates them wrong. I’m trying to fix it.

Thanks

Ok now the images are splitted in the correct way, constructions like the one below will now work:

CCScale9Sprite* buttonBG = CCScale9Sprite::createWithSpriteFrameName("menuButtonTop.png", CCRectMake(5,5,8,8));

here’s the part of the code that is wrong.

*Edit -> Removed: it still fails for other frames on the same sprite sheet

Thanks for the feedback danadn!

Hope the bug will be fixed in the next release.