[Solved] Unable To Hide Stencil Outside of the ClippingNode

Problem
I have created a clipping node with a Sprite using White Image as a Stencil and added Other Sprite Inside Clipping Node to clip it. But I’m having problem hiding stencil outside of the clip.

Here’s an image that show’s the current and expected results.

Clipping Code

// Stencil With White Sprite
auto stencil = Sprite::create("Images/Dummy.png");
stencil->setContentSize(Size(1612+1705, 960));
stencil->setAnchorPoint(Vec2::ZERO);
stencil->setPosition(Vec2(-offset.x - 806, -120));
stencil->setSkewX(40);
this->addChild(stencil);

// Clipping Node
auto clip = ClippingNode::create(stencil);
clip->setAlphaThreshold(0.0f);
this->addChild(clip);

// Sprite Inside Clipping Node That Should Only Show Stencil Overlapping Part
auto dirty_layer = Sprite::create("Images/Part1.png");
dirty_layer->setAnchorPoint(Vec2::ZERO);
dirty_layer->setPosition(offset * -1);
clip->addChild(dirty_layer);

Any help would be appreciated. Thank you in advance.

Is there any particular reason for adding the stencil to the scene? Doing that causes it to be displayed in the scene. If you just need it to be a stencil, then just remove that line.

1 Like

Thanks, It Worked!

I replaced

this->addChild(stencil); 

with

stencil->retain();

and added

stencil->release();

where I wanted to remove it.

You shouldn’t need to call retain()/release() on the stencil, since it’s already done for you in the ClippingNode:create(stencil), which eventually calls ClippingNode::setStencil(Node* stencil), and in there it should be calling retain() on it. If you try to replace the stencil, then it will also handle the call to release() on the current stencil before setting the new one.