定时器

Cocos2dx 3.x Lua 中使用定时器有两种方式:

    1self:scheduleUpdateWithPriorityLua(update, priority)

> 参数一:刷新函数

> 参数二:刷新优先级

        其中 self Node类 的子类。

        该方法默认为每帧都刷新一次,无法自定义刷新时间间隔。

    2scheduler:scheduleScriptFunc(update, inteval, false)

> 参数一:刷新函数

> 参数二:每次刷新的时间间隔

> 参数三:是否只执行一次。false为无限次。

        其中 scheduler 为定时器管理:cc.Director:getInstance():getScheduler()

推荐使用第二种方式,因为比较通用。

 

local function timeLack()

--北京时间2015-12-27 0:0:0   1451145600

   return 1451145600 - tonumber(activeLayer:GetServerTime())

end

local function updateLeftTime(f)

   local t = timeLack()

   if t > 0 then

        local strLeftTime = string.format("%02d:%02d:%02d", math.modf(t % (60 * 60 * 24) / (60 * 60)), math.modf(t % (60 * 60) / 60), t % (60 * 60) % 60)

        local d = math.modf(t / (60 * 60 * 24))

        local strLeftDay = ""

        if d > 0 then

           strLeftDay = string.format("%d", d) .. getLocalString("day", appLoc) .. ""

        end

        textContent:setString(getLocalString("activeTime", appLoc) .. strLeftDay .. strLeftTime)

        else

           textContent:setVisible(false)

        end

   end

   local schedule_updateLeftTime = nil

   local scheduler = cc.Director:getInstance():getScheduler()  --

   schedule_updateLeftTime = scheduler:scheduleScriptFunc(updateLeftTime, 1.0, false) --时间调度器

   local function onNodeEvent(event)  --退出事件停止调用定时器

       if event == "exitTransitionStart" then

          if schedule_updateLeftTime then  

              scheduler:unscheduleScriptEntry(schedule_updateLeftTime)  

              schedule_updateLeftTime = nil

          end

      end

  end

     --创建退出时候监听

     textContent:registerScriptHandler(onNodeEvent)

     updateLeftTime(0)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值