您的位置:首页 > 移动开发 > Cocos引擎

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: