[Solved] Using TextBMFont as ClippingNode stencil

Hi all!

I am trying to create text where upper half of the label is one colour, lower half is another colour. My attempted solution to this problem is to create a clipping node with a text label as a stencil, then attach another coloured flat sprite to render the second colour of the text, but it is not turning out as I intended:

Below is the source code I have used to generate the output:

// mFansLabel is created through cocos studio
TextBMFont* mFansLabel = dynamic_cast<TextBMFont*>(someNode->getChildByName("FansLabel"));
mFansLabel->setPosition(Vec2::ZERO);
mFansLabel->retain();
mFansLabel->removeFromParentAndCleanup(false);
stencilNode->addChild(mFansLabel);

ClippingNode* mFansLabelClippingNode = ClippingNode::create(mFansLabel);
mFansLabelClippingNode->setAlphaThreshold(0.05f);
someNode->addChild(mFansLabelClippingNode);
mFansLabelClippingNode->addChild(mFansLabel);

Size labelSize = mFansLabel->getContentSize();
mFansLabelHalfColourSprite = Sprite::create("sprite/WhitePixel.png");
mFansLabelHalfColourSprite->setScale(2 * labelSize.width, labelSize.height);
mFansLabelHalfColourSprite->setAnchorPoint(Vec2::ANCHOR_MIDDLE_TOP);
mFansLabelHalfColourSprite->setPosition(Vec2::ZERO);
mFansLabelHalfColourSprite->setColor(Color3B::BLACK);
mFansLabelHalfColourSprite->setOpacity(200);
mFansLabelClippingNode->addChild(mFansLabelHalfColourSprite);

Is this a bug with the engine, or is there something I have missed out in the code?
Thanks for any help!!!

You could maybe create two labels and stencil out the front one using a rectangle covering the top half of the main label.

1 Like

Omg this works like a charm! Thanks stevetranby!

1 Like