ClippingNode 跑马灯效果使用
//裁剪标题 做跑马灯
Sprite *stencilSprite = Sprite::create("ui/label_stencil.png");
stencilSprite->setAnchorPoint(Vec2::ZERO);
ClippingNode *clippingNode = ClippingNode::create(stencilSprite);
clippingNode->setInverted(false);
float sWidth = stencilSprite->getContentSize().width;
float sHeight = stencilSprite->getContentSize().height;
Vec2 position = (100, 25);
auto itemname = cocos2d::Label::createWithSystemFont("helloWorld", "Consolas", 16);
itemname->setPosition(Vec2(sWidth / 2, sHeight / 2));
clippingNode->setPosition(Vec2(100 - sWidth * scaleX / 2, 25 - sHeight / 2));
//如果标题过长 则做跑马灯动画
int nWidth = 60;
if (itemname->getContentSize().width > nWidth)
{
itemname->setPosition(Vec2(itemname->getContentSize().width / 2 , sHeight / 2));
itemname->runAction(RepeatForever::create(Sequence::create(
MoveTo::create(3.0f, Vec2(nWidth - itemname->getContentSize().width / 2, sHeight / 2)),
DelayTime::create(1.0f),
MoveTo::create(3.0f, Vec2(itemname->getContentSize().width / 2, sHeight / 2) + SPRITE_OFFSET),
DelayTime::create(1.0f), nullptr
)));
}
clippingNode->addChild(itemname);
addChild(clippingNode, 2, TextItem);
ClippingNode 加上 Label 制作简易跑马灯效果。
拓展版 http://blog.csdn.net/linuxheik/article/details/38258711
简洁版 http://www.cnblogs.com/OrangeLife/p/4373092.html