quick-cocos2d-x学习笔记—定时器

定时器用的地方还是比较多的,游戏中的逻辑判断很多都是采用每帧执行。quick对于schedule的封装在scheduler这个lua文件中。如果是第一次接触quick的话,可能按照官方的api来写一个定时器被报错,提示schedule是一个nil值,这是因为其他的模块在初始化时都是被加载的,唯独这个scheduler没有载入,所以在使用的时候,第一件事是引入这个模块,

 

[html]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. local scheduler = require("framework.scheduler")  


剩下的就可以看着api来写了,在写quick的定时器之前还是再复习一下cocos2dx原生lua对于定时器的写法。

 

每帧调用的,

void scheduleUpdateWithPriority (int priority)

void scheduleUpdateWithPriorityLua (int nHandler,int priority)

 

指定调用间隔时间的,

unsigned int scheduleScriptFunc (unsigned int nHandler, float fInterval, bool bPaused)

 

还有取消定时器事件

void unscheduleScriptEntry (unsigned int uScheduleScriptEntryID)

 

quick的scheduler主要是对后面两个函数的封装。在c++的cocos使用中,我们使用定时器,无非就是每帧调用,间隔时间调用无数次,间隔时间调用指定次数,间隔时间调用一次,取消调用这几个。

我们依次来看下,

每帧调用,

 

[html]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. local time = 0  
  2. local function update(dt)  
  3.     time = time + 1  
  4.     label:setString(string.format("%d", time))  
  5. end  
  6.   
  7. scheduler.scheduleUpdateGlobal(update)  

 

 

间隔一定时间调用,

 

[html]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. local time = 0  
  2. local function onInterval(dt)  
  3.     time = time + 1  
  4.     label:setString(string.format("%d", time))  
  5. end  
  6.   
  7. scheduler.scheduleGlobal(onInterval, 1)  

 

 

间隔时间调用一次,这个封装的不错,很常用

 

[html]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. local time = 0  
  2. local function onInterval(dt)  
  3.     time = time + 1  
  4.     label:setString(string.format("%d", time))  
  5.     print("over")  
  6. end  
  7.   
  8. scheduler.performWithDelayGlobal(onInterval, 1)  


可以看下这个是怎么实现的,

 

 

[html]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. function scheduler.performWithDelayGlobal(listener, time)  
  2.     local handle  
  3.     handle = sharedScheduler:scheduleScriptFunc(function()  
  4.         scheduler.unscheduleGlobal(handle)  
  5.         listener()  
  6.     end, time, false)  
  7.     return handle  
  8. end  


其实就是在间隔一定时间后,将其停止,然后执行一次回调函数就可以了。

 

 

封装的最后一个是停止这些定时器,

 

[html]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. scheduler.unscheduleGlobal()  


它的参数是前面那些定时器返回的句柄,所以如果需要在后面停止掉,记得在创建的留一个返回值就好了。

 

 

不过在游戏中,我们可能会做一个倒计时,也就是间隔一定时间调用指定的次数,这个是在quick中没有封装的,但是我们还是可以自己动手实现一下,原理也很简单,每次执行一次就计个数,达到指定的次数就停止定时器,

 

[html]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. local handle  
  2. local interval = 1  
  3. local repeatIndex = 3  
  4. local index = 0  
  5. local sharedScheduler = CCDirector:sharedDirector():getScheduler()  
  6. handle = sharedScheduler:scheduleScriptFunc(function()  
  7.     index = index + 1  
  8.     label:setString(string.format("%d", index))  
  9.     if index >= repeatIndex then  
  10.         scheduler.unscheduleGlobal(handle)  
  11.         print("over")  
  12.     end  
  13. end, interval, false)  

 

效果如图

 

这样就ok了,大家可以自己试一试哈。

 

定时器就是这样子了。

转载于:https://www.cnblogs.com/damowang/p/5218551.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值