您的位置:首页 > 编程语言 > Lua

lua 自定义控件及动画控制(3) ---- 实现一系列游戏中的消息公告提示

2018-01-29 10:14 363 查看

1 消息上漂提示

实现方法

创建一个Layer图层并加载至场景中;

从消息队列中选择队头消息,加载至Layer并同时移出队列;

利用MoveTo方法将Layer向上移动一定长度(此时Layer中所有子节点消息都随之向上移动),之后将Layer所有子节点位置向上移动同等长度并将Layer位置重置成初始位置,之后递归这一动作,直到消息队列为空;

当向消息队列中添加消息时需要判断消息处理的递归动作是否已经停止,如果已停止,需再次调用递归函数以再次启动处理流程。

实现代码

local UIMessageTip = class("UIMessageTip",_class.UIBase)
function UIMessageTip:ctor()
_class.UIBase.ctor(self)
local cs = cc.Director:getInstance():getWinSize()
self:setContentSize(cs)

self.isMoving = false
self.m_msgArr = {} -- 存储所有进入的消息

self.m_movingLayer = cc.Layer:create() -- 移动图层
self.m_movingLayer:setPosition(cc.p(0,0))
self:addChild(self.m_movingLayer)
end

function UIMessageTip:addMsg( msg ) -- 提供给外部的接口
table.insert(self.m_msgArr, msg)
if not self.isMoving then -- 图层静止 主动showNextMsg
self:showNextMsg()
end
end

UIMessageTip.MsgShowTime = 1.5
UIMessageTip.MsgMoveTime = 0.4
UIMessageTip.LayerMoveY = 30
function UIMessageTip:showNextMsg() -- 尝试显示下一个消息
if #self.m_msgArr <= 0 then
self.isMoving = false
return
end
self.isMoving = true

-- 创建新的text显示msg
local curMsg = table.remove(self.m_msgArr,1)
local msgText = cc.Label:createWithTTF(curMsg.text or "", "fonts/arial.ttf", 32)
msgText:setAnchorPoint(cc.p(0.5,0.5))
local cs = cc.Director:getInstance():getWinSize()
msgText:setPosition(cc.p(cs.width/2,cs.height/2))
self.m_movingLayer:addChild(msgText)
local delayTime = cc.DelayTime:create(self.MsgShowTime)
local callback = cc.CallFunc:create(function (target) -- 实则调用的是CallFuncN
target:removeFromParent()
end)
msgText:runAction(cc.Sequence:create(delayTime,callback)) -- 5秒后移出场景

-- movingLayer向上移动 | 更新已在场景中的msg与movingLayer位置 | 检测下一个
local moveto = cc.MoveTo:create(self.MsgMoveTime,cc.p(0,self.LayerMoveY))
local updateMsgPos = cc.CallFunc:create(function (target)
for i,v in ipairs(target:getChildren()) do
local curPosY = v:getPositionY()
curPosY = curPosY + self.LayerMoveY
v:setPositionY(curPosY)
end
target:setPositionY(0)
end)
local showNextFunc = cc.CallFunc:create(function ()
self:showNextMsg()
end)
self.m_movingLayer:runAction(cc.Sequence:create(moveto,updateMsgPos,showNextFunc))
end

function UIMessageTip:onEnter()
local clipcs = self:getContentSize()
local scrollBtn = ccui.Button:create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png")
scrollBtn:setPosition(cc.p(clipcs.width/2,50))
scrollBtn:addClickEventListener(function(sender)
print("scrollBtn:addClickEventListener")
self:addMsg({text = "i love tt"})
end)
scrollBtn:setTitleText("测试按钮")
self:addChild(scrollBtn)
end
function UIMessageTip:onExit()
end

function UIMessageTipMain()
cclog("UIRotateMenu2Main")
local scene = cc.Scene:create()
local rotateMenu = UIMessageTip:create()
scene:addChild(rotateMenu)
scene:addChild(CreateBackMenuItem())
return scene
end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: