Crop a sprite

Let’s assume there is a sprite running some long duration action. Is there any possibility to crop this site, or mask its part?

Sorry, I don’t really understand what you mean? Would you like to stop the long duration action? There’s CCAction::stop().
Or mask the sprite? CCNode::setTag(int) & getTag()

No, I want to make only a part of some sprite visible (to crop it). One may create a cropped sprite:

CCRect rect = {0, 0, 10, 10}
Sprite* sprite = CCSprite:spriteWithFile("sprite.png", rect);

But what to do if I already have a sprite and for some reason (for example, because it runs some action) don’t want to replace it by a new one? How to make a part of this sprite visible? Or how to make a part of an existing sprite invisible/transparent?

There’s no existing feature satisfying your needs. Creating a new sprite is better. E.g.

CCTexture2D *pTexture = SourceSpriteObj->getTexture();
CGRect rect = {0, 0, 10, 10};
CCSprite *DestSpriteObj = CCSprite::spriteWithTexture(pTexture, CGRect);

Perhaps you can try to add a method setVisibleRect to CCSprite, then modify the flow of CCSprite::draw() function. It needs some skill on OpenGL ES drawing. I’m not good at this.

1 Like