需求:
- 可水平或锤子方向轮播
- 速度,字体,停顿时间可以控制
示例:
--背景
self.layoutBG = ccui.Layout:create()
self.layoutBG:setAnchorPoint(0.5,0.5)
self.layoutBG:setBackGroundColorType(1)--背景颜色显示 0-隐藏 1-显示
self.layoutBG:setBackGroundColor(cc.c3b(0,196,157))--背景颜色
self.layoutBG:setPosition(self.pos)
self.layoutBG:setContentSize(self.size)
self.layoutBG:addTo(self.parentNode)
self.lyBGContentSize = self.layoutBG:getContentSize()
--裁剪可视的内容区域
function RollingNotice:createClippingNoticeNode()
local clipperNode = cc.ClippingNode:create()--创建裁剪的节点
clipperNode:setAnchorPoint(cc.p(0.5,0.5))
clipperNode:setInverted(false)--设置底板(如果为true,模版则会穿透底板。如果false,模版处会显示底板内容)
clipperNode:setStencil(self:createRectangleStencil({
startW = 0,
startH = 0,
width = self.lyBGContentSize.width,
height = self.lyBGContentSize.height,
color = cc.c4b(180,170,160,1),
}))--//设置模板,用来裁剪
clipperNode:setPosition(cc.p(0,0))
clipperNode:addTo(self.layoutBG)
return clipperNode
end
--[[-- 创建矩形区域
@param size 裁剪区域的大小
local drawPolygon ( local verts,
local count,
local fillColor,
local borderWidth,
local borderColor
)
Parameters
verts A pointer to point coordinates.
count The number of verts measured in points.
fillColor The color will fill in polygon.
borderWidth The border of line width.
borderColor The border of line color.
--]]
function RollingNotice:createRectangleStencil( para )
local stencil = cc.DrawNode:create()
local rectanglePoints = {
cc.p(para.startW, para.startH),
cc.p(para.width, para.startH),
cc.p(para.width, para.height),
cc.p(para.startW, para.height)
}
stencil:drawPolygon(rectanglePoints, 4, para.color, 0, para.color)
return stencil
end
function RollingNotice:playRunAction(node)
if not node then return end
local nodeContentSize = node.size
local moveDistance = (node.distanceX or node.distanceY)--总位移
local time = moveDistance / self.moveSpeed --总位移时间
local inPersent,outPersent,delayPersent = 0.5,0.5,0 --进 出 停顿 所占的百分比
--垂直的移动坐标
local moveInPos = cc.p(node.startX,node.startY + moveDistance/2)
local moveDelayPos = cc.p(node.startX,self.lyBGContentSize.height/2)
local moveOutPos = cc.p(node.startX,moveDelayPos.y + moveDistance/2)
if self.director == DIR.ROLLING_DIR_HORIZONTAL then
-- 移动的距离
local inDis = self.lyBGContentSize.width / 2 + nodeContentSize.width
local delayDis = 0
if self.delay and self.delay > 0 then
inDis = node.startX - nodeContentSize.width /2
delayDis = nodeContentSize.width / 2
end
local outDis = moveDistance - inDis - delayDis
--移动的事件比例
inPersent = inDis / moveDistance
delayPersent = delayDis / moveDistance
outPersent = 1 - inPersent - delayPersent
--移动坐标
moveInPos = cc.p(node.startX - inDis ,node.startY)
moveDelayPos = cc.p(moveInPos.x -delayDis,node.startY)
moveOutPos = cc.p(moveDelayPos.x - outDis ,node.startY)
end
--动画
local moveIn = cc.MoveTo:create(time * inPersent,moveInPos)
local fadeIn = cc.FadeIn:create(time * inPersent)
local actionIn = cc.Spawn:create(moveIn,fadeIn)
local actionShow = cc.DelayTime:create(self.delay)
local moveDelay = cc.MoveTo:create(time * delayPersent,moveDelayPos)
local moveOut = cc.MoveTo:create(time * outPersent,moveOutPos)
local fadeOut = cc.FadeOut:create(time * outPersent)
local actionOut = cc.Spawn:create(moveOut,fadeOut)
local callFunc = cc.CallFunc:create(function ()
if self.index >= #self.noticeData then
self.index = 1
else
self.index = self.index + 1
end
--下一条
local node = self:createNoticeTextNode(self.index)
self:playRunAction(node)
end)
local runActs = cc.Sequence:create(actionIn,actionShow,moveDelay,callFunc,actionOut,cc.RemoveSelf:create())
node:runAction(runActs)
end