Cocos2d iPhone - Sprite cliping/mask/frame

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.


I wrote a ClippingNode class which does exactly that. You can add other nodes (sprites, labels, etc.) to the ClippingNode and they will only be drawn in the region specified by the ClippingNode. It also takes device rotation into account.

Internally it uses GL_SCISSOR_TEST like in Bach's answer.

http://www.learn-cocos2d.com/2011/01/cocos2d-gem-clippingnode/