cocos2dx,lua,倒计时的应用
2016-07-26 17:13
435 查看
TimeType=class("TimeType") --获取 00:00:00 时间格式 function TimeType:getTimeString(second) local s=second%60 local min=math.floor(second/60)%60 local h=math.floor(second/3600) local secondBody local minuteBody local hourBody if h==0 and min==0 and s==0 then secondBody="00" elseif s<10 then secondBody="0"..s else secondBody=s end if h==0 and min==0 then minuteBody="00:" elseif min<10 then minuteBody="0"..min..":" else minuteBody=min..":" end if h==0 then hourBody="00:" elseif h<10 then hourBody="0"..h..":" else hourBody=h..":" end return hourBody..minuteBody..secondBody end ---获取 00:00 时间格式 function TimeType:getShortTimeString(second) if second>3600 then return TimeType:getTimeString(second) else local s=second%60 local min=math.floor(second/60)%60 local secondBody local minuteBody if h==0 and min==0 and s==0 then secondBody="00" elseif s<10 then secondBody="0"..s else secondBody=s end if h==0 and min==0 then minuteBody="00" elseif min<10 then minuteBody="0"..min..":" else minuteBody=min..":" end return minuteBody..secondBody end end --得到 0 天 0 小时 0 分 0 秒 function TimeType:getDataString(second) local days=math.floor(second/(60*60*24)) second=second%(60*60*24) local hours=math.floor(second/(60*60)) second=second%(60*60) local minutes=math.floor(second/60) second=second%60 return days.."天"..hours.."小时"..minutes.."分"..second.."秒" end return TimeType
-----------------------------------------------------------------------------------------------------
---倒计时数据的管理
local scheduler = require(cc.PACKAGE_NAME .. ".scheduler")
TimeManager=class("TimeManager")
TimeManager.instance=nil
local _time_map=nil
function TimeManager:ctor()
_time_map = {}
end
function TimeManager:getInstance()
if self.instance==nil then
self.instance=self.new()
end
return self.instance
end
--创建一个倒计时
function TimeManager:createTimer(begin_time,end_time,time_type)
local _time_data={}
_time_data.begin_time=begin_time
_time_data.end_time=end_time
_time_data.time_type=time_type
_time_map[time_type]=_time_data
end
--更新倒计时
function TimeManager:update(dt)
if not _time_map then
return
end
for time_type,class in pairs(_time_map) do
if _time_map[time_type].end_time<os.time() then
self:EndCallBack(time_type)
self:destoryTime(time_type)
end
end
end
--改变时间
function TimeManager:changeTime(time_type,begin_time,end_time)
if not _time_map then
return
end
if not _time_map[time_type] then
self:createTimer(begin_time,end_time,time_type)
else
_time_map[time_type].end_time=end_time
_time_map[time_type].begin_time=begin_time
end
end
function TimeManager:EndCallBack(time_type)
end
--增加倒计时时间
function TimeManager:addTimeValue(time_type,add_value)
local _time_data=_time_map[time_type]
_time_data.end_time=_time_data.end_time+add_value
end
--减少倒计时时间
function TimeManager:reduceTimeValue(time_type,reduve_value)
local _time_data=_time_map[time_type]
_time_data.end_time=_time_data.end_time-reduve_value
end
--设置剩余时间
function TimeManager:getCurTime(time_type)
if not _time_map then
return
end
if not _time_map[time_type] then
return
end
local curTime=os.time()
local cur_value=_time_map[time_type].end_time-curTime
if cur_value<=0 then
cur_value=0
end
print(cur_value)
return cur_value
end
--销毁倒计时
function TimeManager:destoryTime(time_type)
if _time_map[time_type]==nil then
return
end
_time_map[time_type]=nil
end
function TimeManager:release()
_time_map = {}
end
return TimeManager
--------------------------------------------------------------------------------------
-----------倒计时的使用
local startTime=os.time()
local endTime=os.time()+10
TimeManager:getInstance():createTimer(startTime,endTime,TimeManager.TIMEITEM.XINGJUNXIAN_ITEM)
self.label = display.newTTFLabel({
text ="",
size = 64,
color = cc.c3b(255, 0, 0)
})
self.label:setPosition(display.cx,display.cy)
-- self.label:setString(cur_timestr)
self:addChild(self.label)
local add_button=cc.ui.UIPushButton.new({normal=buttonRes.normal,pressed=buttonRes.pressed})
add_button:setPosition(display.cx+100,display.cy+100)
add_button:setButtonLabel("normal", cc.ui.UILabel.new({
UILabelType = 2,
text = "增加时间",
size = 18
}))
add_button:onButtonClicked(function()
TimeManager:getInstance():addTimeValue(TimeManager.TIMEITEM.XINGJUNXIAN_ITEM,100)
end)
self:addChild(add_button)
local del_button=cc.ui.UIPushButton.new({normal=buttonRes.normal,pressed=buttonRes.pressed})
del_button:setPosition(display.cx+100,display.cy-100)
del_button:setButtonLabel("normal", cc.ui.UILabel.new({
UILabelType = 2,
text = "减少时间",
size = 18
}))
del_button:onButtonClicked(function()
TimeManager:getInstance():reduceTimeValue(TimeManager.TIMEITEM.XINGJUNXIAN_ITEM,100)
end)
del_button:addTo(self)
local pause_button=cc.ui.UIPushButton.new({normal=buttonRes.normal,pressed=buttonRes.pressed})
pause_button:setPosition(display.cx-100,display.cy+100)
pause_button:setButtonLabel("normal", cc.ui.UILabel.new({
UILabelType = 2,
text = "暂停",
size = 18
}))
pause_button:onButtonClicked(function()
self:pause()
end)
pause_button:addTo(self)
local resume_button=cc.ui.UIPushButton.new({normal=buttonRes.normal,pressed=buttonRes.pressed})
resume_button:setPosition(display.cx-100,display.cy-100)
resume_button:setButtonLabel("normal", cc.ui.UILabel.new({
UILabelType = 2,
text = "恢复",
size = 18
}))
resume_button:onButtonClicked(function()
self:resume()
end)
local function update(dt)
if self.handle~=nil then
TimeManager:getInstance():update()
self.cur_time = TimeManager:getInstance():getCurTime(TimeManager.TIMEITEM.XINGJUNXIAN_ITEM)
if self.cur_time then
-- print(self.cur_time)
if self.cur_time>=0 then
local cur_time=math.floor(self.cur_time)
local cur_timetype=TimeType:getTimeString(cur_time)
self.label:setString(cur_timetype)
else
-- print(self.cur_time)
self.label:hide()
self.label:removeFromParent()
self:pause()
end
else
self.label:hide()
self.label:removeFromParent()
self:pause()
end
end
end
相关文章推荐
- lua cocos2dx UIManager
- cocos2dx lua Sprite和ImageView切换图片
- cocos2dx 3.12在android studio上从配置到真机运行
- 使用cocos创建的项目,如何进行源码调试?
- 基本控件使用(二)(ScrollView,TableView)
- cocos2dx学习之路----第八篇(初识触摸机制中的单点触摸)
- cocos2dx 在Xcode里面 resource 里面文件夹的搜索
- 关于Cocos2d-x中让主角运动的方法
- cocos2d - JS 工具 - Rect可视化
- cocos2d - JS 屏幕滚动效果
- cocos creator -- 指定自定义事件
- cocos2d-js官方文档 cc.loader
- 使用XCode6.0.1将Cocos2d-x3.1工程打包ipa(支持iOS8)
- 关于cocos2dx虚拟摇杆的lua实现
- cocos2dx-lua制作游戏五维图
- Cocos2dx 截屏功能 3.2以上版本
- cocos2dx C++绑定lua
- Cocos2d-x优化中关于背景图片优化
- cocos2dx3.x物理引擎的碰撞检测
- cocos2d-android - 1.地图动态加载原理(世界地图的实现)