純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")
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章