mxg
mxg

Reputation: 21244

Cocos2d show only a part of a CCSprite

Is there any possibility to show only a part of an CCSprite?

It seams that contentSize property doesn't have a good result.

Upvotes: 4

Views: 4345

Answers (3)

Lukman
Lukman

Reputation: 19129

Both doc_180's and James' answers work by creating new CCSprite using a portion of the texture, but if you are using clipping method, you will get CCSprite that uses the full texture but have the ability to only draw a portion of it on screen. One advantage of this method is you are able to modify how big or small the portion that you want shown or hidden on the fly rather than having to re-create the CCSprite again and again (or replacing the texture again and again).

So, to use the clipping method, simply download the ClippingNode class from here, and add the CCSprite you want clipped to that ClippingNode. Then you call one of its methods to specify which region to limit the drawing to. I'm currently using it to create a progress bar so I know for sure it works great.

Upvotes: 4

billygoat
billygoat

Reputation: 21984

I think you might have to create a new sprite for this. The general pseudo code is this.

CCTexture2D *origTexture = originalSprite->getTexture();

CGRect rect = {0, 0, 20, 20};
CCSprite *destSprite = CCSprite::spriteWithTexture(origTexture, CGRect);

Upvotes: 6

James Webster
James Webster

Reputation: 32066

Get the [sprite displayedFrame], change the frame of that, and create a new sprite with that spriteframe: CCSprite *sprite2 = [CCSprite spriteWithSpriteFrame:frame]

Upvotes: 0

Related Questions