【Lua工作笔记】之一些小的应用模块

这篇博客记录了使用Lua在cocos中实现倒计时功能的方法。首先通过os.time声明截止时间,然后获取当前时间并比较,根据时间差显示倒计时提示。如果时间已过,则显示活动结束信息。博客中提供了详细的代码示例,包括开始计时、定时器更新和停止计时的函数。
摘要由CSDN通过智能技术生成

策划甩了我一个任务,实现倒计时的功能,讲真,以前好像还真没让我做过,也算是新鲜。所以今天就先贴一下,以防将来脑子记不住(开发新手就是爱记这些初级初级再初级的东西,高手见谅啊)

cocos中,有封装好的timer就用封装好的没有就自己写一个(其实就是Scheduler定时器)

1.声明一个截止时间END_TIME ,可以用os.time({year = xxx,month = xxx,day = xxx,hour = xxx,min = xxx,sec = xxx,isdst = false//夏令时间标志})

 在这需要特别声明一下:如果需要将时间设置为系统时间,可以用

localcurDateos.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


哦了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值