views:

105

answers:

1

how can i clip/crop/mask or just set the frame of a CCSprite in Cocos2D?

Something similar to: setting the frame for UIView, with clipping subviews = TRUE

My CCSprite Main Sprite have multiple Child Sprite added to it. I only want Mask part of that Main Sprite Sprite visible. Is there a way to clip or use a mask for CCSprite?

I could cut the background and layer that on top, leaving only that visible area, but is that the only way?!

here's a sample image demonstrating what I'm trying to achieve: alt text

+2  A: 

I ended up using GL_SCISSOR.

in MainSprite I impemented:

- (void) visit
{
    if (!self.visible) {
        return;
    }
    glEnable(GL_SCISSOR_TEST);
    glScissor(x, y, width, height);   
    [super visit];
    glDisable(GL_SCISSOR_TEST);
}

This will clip or mask the specified area.

The only tricky bit is that in Landscape mode Cocos2D has 0,0 at the bottom-left side of the screen, while OpenGL has it at the bottom-right corner as it doesn't consider the orientation of the screen.

In other words, for OpenGL consider you have a rotated portrait Screen.

Bach
One other caveat - the clipping region is not relative to the sprite, it's world-based. So, it will remain in place no matter how you transform the sprite. You'll have to manually move/scale it along with the sprite if you need the mask to move.
jtalarico
Yeah you're right. I didn't realize that at first. Now I made the x and y relative to my view's position so they always move together.
Bach