纯Lua事件分发模块

来源:http://www.cocos2d-lua.org/doc/event/index.md

 

PushCenter.lua

可在https://github.com/u0u0/Lua-utils/blob/master/src/app/utils/PushCenter.lua获取热更新核心逻辑代码。

初始化

PushCenter 是单例设计的,为了避免被GC,导致内存存储的事件注册信息丢失,你需要把PushCenter 挂在一个全局变量上。如下:

app.PushCenter = require("app.utils.PushCenter")

注册监听

local PushCenter = require("app.utils.PushCenter")

PushCenter.addListener("LoginEvent", function(param)
	dump(param)
end, self)

其中 self 为监听者,当self销毁的时候,你需要注销监听。

function MainScene:onExit()
	PushCenter.removeListenersByTag(self)
end

removeListenersByTag可以一次性取消self的所有事件注册。

发送事件

PushCenter.pushEvent("LoginEvent", "this is param")
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章