1
votes

I am looking for a way to mask out a layer and reveal the contents of the layer through a rectangle (actually I want multiple rectangles to reveal the underlying layer's content).

I came across this similar question: Cocos2d iPhone - Sprite cliping/mask/frame

Which had a solution: http://www.learn-cocos2d.com/2011/01/cocos2d-gem-clippingnode/

However, when trying to use this class, I get the warning: "implicit declaration of function 'glPushMatrix' is invalid in C99"... I also get the error: "Property 'deviceOrientation' not found on object of type 'CCDirector *'"

What do I need to do to get this to work with the latest version of cocos2d?

... In any event, I commented out the deviceOrientation stuff, just to test if it will even work, and it doesn't seem to be.

I've got a CCBatchNode:

    sheet = [CCSpriteBatchNode batchNodeWithFile:@"bg.png" capacity:500];

Then I add many sprites to that

    [sheet addChild:sprite1];
    [sheet addChild:sprite2];
    [sheet addChild:sprite3];

Then I make the clipping node layer

ClippingNode *clipNode = [ClippingNode node];
clipNode.clippingRegion = CGRectMake(50, 50, 200, 200);

Then I add the sprite sheet and the clipNode:

[layer addChild:sheet];
[layer addChild:clipNode];

Then I add that to the CCSprite object

[self addChild:layer];

...

The result is, I see my many sprites from the sheet, but there is no clipping mask.. And my console shows a million: "OpenGL error 0x0502 in -[CCTextureAtlas drawNumberOfQuads:fromIndex:] 556"

so..... I am not sure what I am doing wrong-- or if this all has to do with the openGL warnings and device orientation errors... ?

UPDATE: I added #include <OpenGLES/ES1/gl.h> to the ClippingNode.m, and it got rid of the glpush/pop warnings.. But still results in the same OpenGL error once I add the clipNode child to the layer...

1

1 Answers

0
votes

You can do it with shaders in cocos2d 2.0. Have a look at this tutorial.

http://www.raywenderlich.com/4428/how-to-mask-a-sprite-with-cocos2d-2-0