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
相关文章推荐
- cocos2dx - lua 自定义控件及动画控制(2) ---- 基于Action实现循环滚动效果
- 跑酷类游戏------quick-Cocos2d-X lua添加plist文件以及实现动画
- 游戏Loading中的小提示和Loading动画实现
- 利用观察者模式实现Cocos2DX-lua游戏中的消息管理系统
- 纯JavaScript基于notie.js插件实现消息提示特效
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(八) 完美实现A*寻径动态动画
- 【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现CCAnimate动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)! .
- 游戏中的路径动画设计与实现
- MFC实现类似qq的在屏幕右下角动画显示消息窗口 .
- Unity3D 发送广播与消息、利用脚本控制游戏
- ajax 动态获取!实现站内消息提示!
- ajax 无刷新 实现页面右下角弹出消息提示框
- cocos2d+lua实现帧动画播放
- 利用DecorView实现播放视频开灯/关灯效果和仿QQ消息提示框
- Android游戏开发22:Android动画的实现--J2me游戏类库用于Android开发
- Android自定义控件:动画类(十)----AnimatorSet联合动画的代码实现
- iphone游戏开发之cocos2d (七) 自定义精灵类,实现精灵动画CCAnimation
- 控制DIV内容滚动的方法,实现不用拖滚动条就可以看到最新消息
- 自定义控件-实现ListView Item进入动画