Masking moving sprite with static sprite mask OR using a sprite mask to stencil a layer

Hi. I am attempting to render a complex space scene in which I have many sprites. I am using Cocos2d-x 2.2. I need the ability to mask a moving sprite with a static sprite or stencil a layer in the shape of the mask, both of which I am having problems with. I have been able to get the moving mask working but it relies on me shifting and updating the moving sprite every frame which becomes very slow. The code I am using for this is from https://github.com/syuhari/CCMask. I have also taken a look at the option of having a stencil but have only found examples of stenciling out rectangles and simple shapes. What I need is to stencil out a sprite.

Here are some example images to demonstrate:

I want this:

given the stars background and this mask

Yes, I realize I can get away without using a mask in this first example here but I also need to mask clouds moving on the planet and if I need two masks, simply layer hiding won’t work. A moving mask or a layer mask with a sprite stencil is exactly what I am after.

My question:

Does anyone know how to mask a sprite with another sprite and allow the sprite to move while the mask stays put.

OR

Does anyone know how I would use the mask image I provided to create a clipping layer or stencil or whatever it is called so that it would not draw anything on the star layer past the start of the planet.

Thank you for your time. Also, if I am confused in any terminology, please correct me. I’m here to learn.

Chanz

@chanz

To mask a sprite with another sprite should be easy, you could take a look at the ClippingNodeTest in the engine’s tests.

If you want the sprite to move while the mask stays put, you can update the mask position to the sprite position every frame.

@owen wrote:

@chanz

To mask a sprite with another sprite should be easy, you could take a look at the ClippingNodeTest in the engine’s tests.

If you want the sprite to move while the mask stays put, you can update the mask position to the sprite position every frame.

Thanks for the reply. I have taken a look at the ClippingNodeTest in the TestCPP project but can’t find where a sprite is used to mask another sprite. Can you elaborate on how to do this? Am I masking the actual sprite or the layer the sprite is on? Can you point me to where in the ClippingNodeTest the example you mentioned is.

Thank you so much.

@chanz

//this is a sprite need to mask
    heroCard = CCSprite::create(cardName);
 
    //this is a mask sprite
    mask = CCSprite::createWithSpriteFrameName("xxx");
    mask->setPosition(heroCard->getPosition());
    
    //settings up the masks
    CCClippingNode *holseClipper = CCClippingNode::create();
    holseClipper->setInverted(true);
    holseClipper->setAlphaThreshold(0);
    holseClipper->addChild(heroCard);
    
    CCNode *node = CCNode::create();
    node->addChild(mask);
    holseClipper->setStencil(node);

    this->addChild(holseClipper, buttonTouxiang->getZOrder() - 1);

//set the mask position to the heroCard
    heroCard->setPosition(xxx);
    mask->setPosition(heroCard->getPosition());

@owen Thank you so much. Much appreciated.