Forum Libraries, Components, and Plugins


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Can't get CCClippingNode to work - has anyone?

Hi, I'm wondering if anyone can help me with this... basically I've got a sprite that I want to reveal by using a mask that get's updated upon certain actions in the game.

So I'm using a CCDrawNode for the CCClippingNode stencil and a sprite for the content. No matter what I do the whole sprite gets drawn. Here's some example code which should only reveal a small rectangular area of the full image:

CCSprite fullImage = new CCSprite("myphoto");

CCDrawNode revealMask = new CCDrawNode();
revealMask.DrawRect(new CCRect(100.0F, 100.0F, 300.0F, 200.0F), CCColor4B.White);

CCClippingNode revealedImage = new CCClippingNode();
revealedImage.ContentSize = ActualBackground.ContentSize;
revealedImage.AnchorPoint = new CCPoint(0.5f, 0.5f);
revealedImage.Stencil = revealMask;


I have also tried setting the CCClippingNode alpha threshold to 0.0 as this is what some COCOS2D examples use and this has no effect.

I then tried downloading the CocosSharp source code from GitHub and compiled and ran the suite of tests that is included with it and I have to say that I don't think that the CCClippingNode tests are working - these don't seem to be clipping either. Is something broken in CocosSharp.

I've tried using both the 1.2 and the newly updated 1.3 versions. I'm running on iOS using the simulator if that makes any difference.

Any help will be gratefully appreciated,

Best Answers


  • Thanks for confirming this. I can live with this for a while as I can swap my main testing from iOS to Android. However this is quite key to my game, is this likely to be fixed in the next couple of months?

  • OK - thank you again. I'll try a different approach using a render texture to try and blend a draw node with the background image and save the resultant texture into a sprite. Unless the problem is to do with the BlendFunc as I think I need to use CCBlendFunc.Opaque for the mask and then CCBlendFunc.AlphaBlend for my background.

  • AdrianNelson.0022AdrianNelson.0022 GBMember ✭✭

    Now with the CocosSharp 1.3.2 update it is now possible to create a masked sprite using a draw node mask as follows:

        public static CCSprite CreateMaskedSprite(CCSprite image, CCDrawNode mask)
            // see
            // remember current values to re-instate
            CCPoint     maskPosition        = mask.Position;
            CCPoint     maskAnchorPoint     = mask.AnchorPoint;
            CCBlendFunc maskBlendFunc       = mask.BlendFunc;
            CCPoint     imagePosition       = image.Position;
            CCPoint     imageAnchorPoint    = image.AnchorPoint;
            CCBlendFunc imageBlendFunc      = image.BlendFunc;
            // ensure we have setup what we need to correctly create masked sprite
            mask.Position       = CCPoint.Zero;
            mask.AnchorPoint    = CCPoint.AnchorLowerLeft;
            mask.BlendFunc      = new CCBlendFunc(CCOGLES.GL_ONE, CCOGLES.GL_ZERO);
            image.Position      = CCPoint.Zero;
            image.AnchorPoint   = CCPoint.AnchorLowerLeft;
            image.BlendFunc     = new CCBlendFunc(CCOGLES.GL_DST_ALPHA, CCOGLES.GL_ZERO);
            // create the masked image
            CCRenderTexture rt  = new CCRenderTexture(image.ContentSize, image.TextureRectInPixels.Size, CCSurfaceFormat.Color, CCDepthFormat.Depth24Stencil8);
            // re-instate values
            mask.Position       = maskPosition;
            mask.AnchorPoint    = maskAnchorPoint;
            mask.BlendFunc      = maskBlendFunc;
            image.Position      = imagePosition;
            image.AnchorPoint   = imageAnchorPoint;
            image.BlendFunc     = imageBlendFunc;
            // return masked sprite
            return new CCSprite(rt.Sprite.Texture);

    Hope that helps.

Sign In or Register to comment.