coco2d-lua 自定义广播轮播的控件

需求:

  1. 可水平或锤子方向轮播
  2. 速度,字体,停顿时间可以控制

示例:

	--背景
	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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值