实现这个字幕效果,其实很简单,只需要画一个遮罩即可完成,带遮罩内部显示,外部隐藏,如下有C++,lua两个版本的代码:
function GameClientView:updateAdvertisement()
-- body
local GG = self._Panel:getChildByName("notification_bg")
local GGW = GG:getContentSize().width
local GGH = GG:getContentSize().height
local OrgX = GG:getPositionX() - GGW / 2
local OrgY = GG:getPositionY() - GGH / 2 - 10
local DesX = GG:getPositionX() + GGW / 2 - 70
local DesY = GG:getPositionY() + GGH / 2
local voice_icon = GG:getChildByName("voice_icon") --呐吧
local text = cc.Label:createWithTTF("首次充值将会活动额外奖励", "fonts/fnt_forestparty.TTF", 20)
text:setPosition(cc.p(DesX + DesX / 2, GGH / 2))
local clip = cc.ClippingNode:create()
GG:addChild(clip)
-- --以下模型是带图像遮罩
local bgSharp = cc.DrawNode:create()
local beginX = voice_icon:getContentSize().width
local Point = {
[1] = cc.p(beginX / 2, 0),
[2] = cc.p(GGW - beginX / 2, 0),
[3] = cc.p(GGW - beginX / 2, GGH),
[4] = cc.p(beginX / 2, GGH),
}
bgSharp:drawPolygon(Point, 4, cc.c4f(1, 0, 0, 1), 2, cc.c4f(0, 1, 0, 1))
clip:setStencil(bgSharp)
clip:setAnchorPoint(cc.p(0.5, 0.5))
clip:setPosition(beginX / 2, 0)
clip:addChild(text)
local function displayAdvise()
-- body
if text:getPositionX() < 0 then
--todo
text:setPosition(cc.p(DesX + DesX / 2, GGH / 2))
else
text:setPositionX(text:getPositionX() - 10)
end
end
self._adviseTimerHandle = cc.Director:getInstance():getScheduler():scheduleScriptFunc(displayAdvise, 0.1, false)
end
C++版本
bool HelloWorld::init()
{
CCSize size = CCDirector::sharedDirector()->getVisibleSize();
//创建要显示的文字
text = CCLabelTTF::create("ddddddddddddddddddddddddddddddddddddddddd", "", 30);
text->setPosition(ccp(20, 80));
//this->addChild(text);
//绘制裁剪区域
CCDrawNode* shap = CCDrawNode::create();
CCPoint point[4] = { ccp(80, 60), ccp(200, 60), ccp(200, 200), ccp(80, 200) };
shap->drawPolygon(point, 4, ccc4f(355, 255, 255, 255), 2, ccc4f(255, 255, 255, 255));
CCClippingNode* cliper = CCClippingNode::create();
cliper->setStencil(shap);
cliper->setAnchorPoint(ccp(.5, .5));
cliper->setPosition(ccp(120, 80));
addChild(cliper);
//把要滚动的文字加入到裁剪区域
cliper->addChild(text);
//文字滚动,超出范围后从新开始
this->schedule(schedule_selector(HelloWorld::updateMove), 0.1);
return true;
}
void HelloWorld::updateMove(float f)
{
if (text->getPositionX() > 120)
{
text->setPositionX(20);
}
text->setPositionX(text->getPositionX() + 10);
}
注:利用DrawNode画出遮罩的模版出来就可以了、。调试模版的形状看个人需求去调试即可、