https://img-blog.csdnimg.cn/32db9ce43ef64316a2e37a31f4cee033.gif
编程小鱼酱yu612.com,点击前往

cocos-lua学习笔记(十)事件---调度器

第一种是 全局调度器

全局帧调度器

每一帧描画都会触发,windows上窗口移动和最小化的时候会暂停描画。

scheduler.scheduleUpdateGlobal(onInterval) 每一帧调用

全局自定义调度器

指定调度器的时间

scheduler.scheduleGlobal(onInterval, 0.5)  0.5秒调用一次


全局延时调度器

等待指定时间后执行一次回调函数

scheduler.performWithDelayGlobal(onInterval, 0.5)  延时0.5秒调用


local MainScene = class("MainScene", function()
    return display.newScene("MainScene")
end)

local scheduler = require(cc.PACKAGE_NAME .. ".scheduler")

function MainScene:ctor()
	-- 全局帧调度器
	local function onInterval(dt)
		print("update")
	end
	scheduler.scheduleUpdateGlobal(onInterval)

	-- 全局自定义调度器
	local function onInterval(dt)
		print("Custom")
	end
	scheduler.scheduleGlobal(onInterval, 0.5)

	-- 全局延时调度器
	local function onInterval(dt)
		print("once")
	end
	scheduler.performWithDelayGlobal(onInterval, 0.5)
end

return MainScene

大家看一下控制台吧,感觉像是爆炸了。



第二种要介绍的则是 节点调度器

Node是基础类,封装很多基础的方法和属性,其中调度器就是方法之一。

Node中的调度器只能在Node中使用,Node负责管理调度器的生命周期。

大部分情况下,我们使用节点调度器,这样就能把精灵集中在游戏逻辑实现,而不是调度器的生命周期管理。


1.节点桢调度器

2.节点自定义调度器

3.节点延时调度器

local MainScene = class("MainScene", function()
    return display.newScene("MainScene")
end)

local scheduler = require(cc.PACKAGE_NAME .. ".scheduler")

function MainScene:ctor()
	self:schedule(function () print("schedule") end, 1.0)

	self:performWithDelay(function () print("performWithDelay") end, 1.0)
end

return MainScene


停止节点的方法是

node:stopAction(action)


posted @ 2017-11-21 09:29  鱼酱  阅读(454)  评论(0编辑  收藏  举报

https://img-blog.csdnimg.cn/32db9ce43ef64316a2e37a31f4cee033.gif
编程小鱼酱yu612.com,点击前往