Search code examples
cocos2d-iphone

Cocos2d iPhone - Sprite cliping/mask/frame


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
(source: dnamique.com)


Solution

  • 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.