开发者

Cocos2d show only a part of a CCSprite

开发者 https://www.devze.com 2023-04-03 01:47 出处:网络
Is there any possibility to show o开发者_JS百科nly a part of an CCSprite? It seams that contentSize property doesn\'t have a good result.I think you might have to create a new sprite for this. The ge

Is there any possibility to show o开发者_JS百科nly a part of an CCSprite?

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


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);


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号