条条大路通罗马,其实实现的方法有多种,下面我就写2种吧。。
第1种:通过ClippingNode画一个遮罩即可完成,带遮罩内部显示,外部隐藏.
local NoticeBg = self:getWidget("Image_notice") local NoticeBgW = NoticeBg:getContentSize().width local NoticeBgH = NoticeBg:getContentSize().height local OrgX = - NoticeBgW / 2 local OrgY = NoticeBgH / 2 local DesX = NoticeBgW + 100 local DesY = NoticeBgH / 2 local voice_icon = self:getWidget("Image_voice") --呐吧 local text = cc.Label:createWithTTF("首次充值将会活动额外奖励","zhunyuan.ttf",26) text:setPosition(cc.p(OrgX,OrgY)) text:setAnchorPoint(0,0.5) text:setColor(cc.c3b(255,255,255)) local clip = cc.ClippingNode:create() NoticeBg:addChild(clip) -- --以下模型是带图像遮罩 local bgSharp = cc.DrawNode:create() local beginX = voice_icon:getPositionX()+voice_icon:getContentSize().width local Point = { [1] = cc.p(beginX / 2,0),[2] = cc.p(NoticeBgW - beginX,[3] = cc.p(NoticeBgW - beginX,NoticeBgH),[4] = cc.p(beginX / 2,} bgSharp:drawPolygon(Point,4,cc.c4f(1,1),2,cc.c4f(0,1,1)) clip:setStencil(bgSharp) clip:setAnchorPoint(cc.p(0.5,0.5)) clip:setPosition(beginX / 2,0) clip:addChild(text) local function displayAdvise() if text:getPositionX()+text:getContentSize().width+10 < 0 then --todo text:setPosition(cc.p(DesX,DesY)) else text:setPositionX(text:getPositionX() - 3) --移动速度 end end self.schedu = scheduler:scheduleScriptFunc(displayAdvise,0.01,false)
第2种:通过scrollView实现。在scrollView里面添加一个text即可。
self._scrollView = self:getWidget("ScrollView_2") self._scrollText = self._scrollView:getChildByName("Text_3") self._scrollText:setText("我是测试公告我是测试公告") local posX,posY = self._scrollText:getPosition() local function callback() self._scrollText:setPosition(posX,posY) self._scrollText:moveTo({x=-200,y=self._scrollText:getPositionY(),time=10,onComplete=callback}) end self._scrollText:moveTo({x=-200,onComplete=callback})
原文链接:https://www.f2er.com/cocos2dx/338816.html