code
November 19, 2012, 9:04pm
#1
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) )
dbrain
January 15, 2013, 11:16am
#2
Did you ever find a solution to this problem? I’m trying to ninepatch scale an image and getting the same result
code
January 19, 2013, 3:54pm
#3
No … i made a copy of the file and modified the source where it needed to be.
denranx
January 23, 2013, 3:23pm
#4
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?
dbrain
January 23, 2013, 9:15pm
#5
You need to setContentSize. In my understanding the CCScale9Sprite scales around the content, if there is none it just doesn’t show anything.
denranx
January 24, 2013, 4:06pm
#6
I’ve tested with setContentSize & setPreferredSize but still the same issue…
code
January 26, 2013, 6:45am
#7
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.
danadn
February 11, 2013, 1:30am
#8
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
denranx
February 11, 2013, 9:28am
#9
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) );
danadn
February 11, 2013, 10:48am
#10
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?
denranx
February 11, 2013, 11:58am
#11
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.
danadn
February 11, 2013, 4:11pm
#12
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
denranx
February 11, 2013, 4:47pm
#13
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.
danadn
February 11, 2013, 5:15pm
#14
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
danadn
February 11, 2013, 5:52pm
#15
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
denranx
February 11, 2013, 8:49pm
#16
Thanks for the feedback danadn!
Hope the bug will be fixed in the next release.