You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.0 KiB

  1. --[[
  2. Copyright (c) 2011-2014 chukong-inc.com
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. ]]
  19. --------------------------------
  20. -- @module scheduler
  21. --[[--
  22. 全局计时器、计划任务
  23. «该模块在框架初始化时不会自动载入»
  24. 加载方式: local scheduler = require(cc.PACKAGE_NAME .. ".scheduler")
  25. ]]
  26. local scheduler = {}
  27. local sharedScheduler = cc.Director:getInstance():getScheduler()
  28. -- start --
  29. --------------------------------
  30. -- 计划一个全局帧事件回调,并返回该计划的句柄。
  31. -- @function [parent=#scheduler] scheduleUpdateGlobal
  32. -- @param function 回调函数
  33. -- @return mixed#mixed ret (return value: mixed) schedule句柄
  34. --[[--
  35. 计划一个全局帧事件回调,并返回该计划的句柄。
  36. 全局帧事件在任何场景中都会执行,因此可以在整个应用程序范围内实现较为精确的全局计时器。
  37. 该函数返回的句柄用作 scheduler.unscheduleGlobal() 的参数,可以取消指定的计划。
  38. ]]
  39. -- end --
  40. function scheduler.scheduleUpdateGlobal(listener)
  41. return sharedScheduler:scheduleScriptFunc(listener, 0, false)
  42. end
  43. -- start --
  44. --------------------------------
  45. -- 计划一个以指定时间间隔执行的全局事件回调,并返回该计划的句柄。
  46. -- @function [parent=#scheduler] scheduleGlobal
  47. -- @param function listener 回调函数
  48. -- @param number interval 间隔时间
  49. -- @return mixed#mixed ret (return value: mixed) schedule句柄
  50. --[[--
  51. 计划一个以指定时间间隔执行的全局事件回调,并返回该计划的句柄。
  52. ~~~ lua
  53. local function onInterval(dt)
  54. end
  55. -- 每 0.5 秒执行一次 onInterval()
  56. local handle = scheduler.scheduleGlobal(onInterval, 0.5)
  57. ~~~
  58. ]]
  59. -- end --
  60. function scheduler.scheduleGlobal(listener, interval)
  61. return sharedScheduler:scheduleScriptFunc(listener, interval, false)
  62. end
  63. -- start --
  64. --------------------------------
  65. -- 取消一个全局计划
  66. -- @function [parent=#scheduler] unscheduleGlobal
  67. -- @param mixed schedule句柄
  68. --[[--
  69. 取消一个全局计划
  70. scheduler.unscheduleGlobal() 的参数就是 scheduler.scheduleUpdateGlobal() 和 scheduler.scheduleGlobal() 的返回值。
  71. ]]
  72. -- end --
  73. function scheduler.unscheduleGlobal(handle)
  74. sharedScheduler:unscheduleScriptEntry(handle)
  75. end
  76. -- start --
  77. --------------------------------
  78. -- 计划一个全局延时回调,并返回该计划的句柄。
  79. -- @function [parent=#scheduler] performWithDelayGlobal
  80. -- @param function listener 回调函数
  81. -- @param number time 延迟时间
  82. -- @return mixed#mixed ret (return value: mixed) schedule句柄
  83. --[[--
  84. 计划一个全局延时回调,并返回该计划的句柄。
  85. scheduler.performWithDelayGlobal() 会在等待指定时间后执行一次回调函数,然后自动取消该计划。
  86. ]]
  87. -- end --
  88. function scheduler.performWithDelayGlobal(listener, time)
  89. local handle
  90. handle = sharedScheduler:scheduleScriptFunc(function()
  91. scheduler.unscheduleGlobal(handle)
  92. listener()
  93. end, time, false)
  94. return handle
  95. end
  96. return scheduler
  97. --scheduler_lua--scheduler.lua