策划甩了我一个任务,实现倒计时的功能,讲真,以前好像还真没让我做过,也算是新鲜。所以今天就先贴一下,以防将来脑子记不住(开发新手就是爱记这些初级初级再初级的东西,高手见谅啊)
cocos中,有封装好的timer就用封装好的没有就自己写一个(其实就是Scheduler定时器)
1.声明一个截止时间END_TIME ,可以用os.time({year = xxx,month = xxx,day = xxx,hour = xxx,min = xxx,sec = xxx,isdst = false//夏令时间标志})
在这需要特别声明一下:如果需要将时间设置为系统时间,可以用
localcurDate= os.date("*t")
local END_TIME = os.time({year = tonumber(curDate.year), month = tonumber(curDate.month), day = tonumber(curDate.day), hour = 24, min = 0,sec = 0, isdst = false})
*tips1
可以print出date{}的内容
local t = os.date("*t",os.time())for k,v in pairs(t) do
print("----------------------------------")
print(k,v)
end
输出结果:
*tips2
关于参数的问题,贴个链接,觉得大大讲得比我全面周到得多 (多向前辈学习嘛) http://blog.csdn.net/henren555/article/details/18216839
2.获取当前的时间(os.time),根据需要去写:比如这里需要显示截止到xx之前的时间,故:
function TestView:showTip()
local curTime = os.time()
if curTime < END_TIME then
self.timeTips:setString("blahblahblah")//没什么卵用,纯粹用来标志运行的
self.timerEnd = END_TIME
self:startTimer()
return
end
self.timeTips:setString("活动已结束,下次再来参与吧!")
end
function TestView:startTimer()----//开始计时(开启调度器)
local function runt()
local curTime = os.time()
local lasTime = self.timerEnd - curTime
local time = formatTime(lasTime)
print("time"..time)
if self.timerEnd == END_TIME then
if lasTime < 0 then
self.timeTips:setString("活动已结束,下次再来参与吧!")
self:killTimer()
else
self.timeTips:setString("距离活动截止还有:"..time)
end
end
end
runt()
self.timerId = Timer:start(function()
runt()
end, 1)//每一秒执行触发一次
end
function TestView:killTimer()-----unScheduler
if self.timerId then
Timer:kill(self.timerId)
self.timerId = nil
end
end
哦了!