cc.bind(self, "event")
这句是干什么的呢
就是将
I:\flipCard\simulator\win32\src\cocos\framework\components\event.lua
event文件中的
事件
local Event = class("Event")
local EXPORTED_METHODS = {
"addEventListener",
"dispatchEvent",
"removeEventListener",
"removeEventListenersByTag",
"removeEventListenersByEvent",
"removeAllEventListeners",
"hasEventListener",
"dumpAllEventListeners",
}
绑定到self的身上 让self可以直接使用
比如
self:removeAllEventListeners()
self.gameView_ = GameView:create()
:addEventListener(GameView.events.PLAYER_DEAD_EVENT, handler(self, self.onPlayerDead))
:start()
:addTo(self)
self:dispatchEvent({name = GameView.events.PLAYER_DEAD_EVENT})
写了个示例代码
function SelModeScene:onCreate()
--printf("resource node = %s", tostring(self:getResourceNode()))
-- bind the "event" component
cc.bind(self, "event") --添加绑定事件 让self 拥有这些事件函数
self:addEventListener("PLAYER_DEAD_EVENT", handler(self, self.onPlayerDead))--添加通知事件
self:dispatchEvent({name = "PLAYER_DEAD_EVENT"})--启动通知 调用事先绑定好的通知函数self.onPlayerDead
self:dumpAllEventListeners()--打印所有的事件 这个时候是有货的
self:removeAllEventListeners()--清空所有事件 还有其他接口可以根据name删之类的
self:dumpAllEventListeners()--再次打印 已经没有
self:dispatchEvent({name = "PLAYER_DEAD_EVENT"})--再次通知 无效通知 不会执行self.onPlayerDead
cc.unbind(self, "event")--解绑
self:addEventListener("HEHE", handler(self, self.onPlayerDead))--解绑之后 调用事件函数错误
end
function SelModeScene:onPlayerDead()
print("通知了执行 onPlayerDead 函数")
end
如果不绑定 使用通知貌似很复杂
查看了3.10的lua源码 他是这样玩的
local function eventCustomListener1(event)
local str = "Custom event 1 received, "..event._usedata.." times"
statusLabel1:setString(str)
end
local listener1 = cc.EventListenerCustom:create("game_custom_event1",eventCustomListener1)
self._listener1 = listener1
local eventDispatcher = self:getEventDispatcher()
eventDispatcher:addEventListenerWithFixedPriority(listener1, 1)
local function sendCallback1(tag, sender)
count1 = count1 + 1
local event = cc.EventCustom:new("game_custom_event1")
event._usedata = string.format("%d",count1)
eventDispatcher:dispatchEvent(event)
end
local sendItem1 = cc.MenuItemFont:create("Send Custom Event 1")
sendItem1:registerScriptTapHandler(sendCallback1)
sendItem1:setPosition(cc.p(origin.x + size.width/2, origin.y + size.height/2))
添加多个事件 传参数
function SelModeScene:onCreate()
--printf("resource node = %s", tostring(self:getResourceNode()))
-- bind the "event" component
cc.bind(self, "event") --添加绑定事件 让self 拥有这些事件函数
self:addEventListener("PLAYER_DEAD_EVENT1", handler(self, self.onPlayerDead))--添加通知事件
self:dispatchEvent({name = "PLAYER_DEAD_EVENT1", level = 5, integral = 60})--启动通知 调用事先绑定好的通知函数self.onPlayerDead
self:addEventListener("PLAYER_DEAD_EVENT2", handler(self, self.onPlayerDead2))--添加通知事件
self:dispatchEvent({name = "PLAYER_DEAD_EVENT2", attr = 350})--再次通知 无效通知 不会执行self.onPlayerDead
end
function SelModeScene:onPlayerDead(event)
print("通知了执行 onPlayerDead 函数")
dump(event, "event = ")
end
function SelModeScene:onPlayerDead2(event)
print("通知了执行 onPlayerDead2 函数")
dump(event, "event = ")
end
输出
[LUA-print] [INFO] userdata: 0x09425d68 [Event] addEventListener() - event: PLAY
ER_DEAD_EVENT1, handle: 1, tag: ""
[LUA-print] [INFO] userdata: 0x09425d68 [Event] dispatchEvent() - event PLAYER_D
EAD_EVENT1
[LUA-print] [INFO] userdata: 0x09425d68 [Event] dispatchEvent() - dispatching ev
ent PLAYER_DEAD_EVENT1 to listener 1
[LUA-print] 通知了执行 onPlayerDead 函数
[LUA-print] dump from: [string ".\app/scenes/SelModeScene.lua"]:37: in function
<[string ".\app/scenes/SelModeScene.lua"]:35>
[LUA-print] - "event = " = {
[LUA-print] - "integral" = 60
[LUA-print] - "level" = 5
[LUA-print] - "name" = "PLAYER_DEAD_EVENT1"
[LUA-print] - "stop" = function: 0x0904e610
[LUA-print] - "stop_" = false
[LUA-print] - "tag" = ""
[LUA-print] - "target" = userdata: 0x09425d68
[LUA-print] - }
[LUA-print] [INFO] userdata: 0x09425d68 [Event] addEventListener() - event: PLAY
ER_DEAD_EVENT2, handle: 2, tag: ""
[LUA-print] [INFO] userdata: 0x09425d68 [Event] dispatchEvent() - event PLAYER_D
EAD_EVENT2
[LUA-print] [INFO] userdata: 0x09425d68 [Event] dispatchEvent() - dispatching ev
ent PLAYER_DEAD_EVENT2 to listener 2
[LUA-print] 通知了执行 onPlayerDead2 函数
[LUA-print] dump from: [string ".\app/scenes/SelModeScene.lua"]:42: in function
<[string ".\app/scenes/SelModeScene.lua"]:40>
[LUA-print] - "event = " = {
[LUA-print] - "attr" = 350
[LUA-print] - "name" = "PLAYER_DEAD_EVENT2"
[LUA-print] - "stop" = function: 0x093e3ba8
[LUA-print] - "stop_" = false
[LUA-print] - "tag" = ""
[LUA-print] - "target" = userdata: 0x09425d68
[LUA-print] - }
quick 3.3 是这样添加的
cc.GameObject.extend(self):addComponent("components.behavior.EventProtocol"):exportMethods()
公司项目是这样添加的
function AppBase:ctor(appName, packageRoot)
cc(self):addComponent("components.behavior.EventProtocol"):exportMethods()
--或者
local timer = {}
cc(timer):addComponent("components.behavior.EventProtocol"):exportMethods()
timer:dispatchEvent({name = eventName, countdown = 0})
--自己的类使用
function PXUISkill:initUI()
cc(self):addComponent("components.behavior.EventProtocol"):exportMethods()
local function callback(event)
dump(event, "event")
end
self:addEventListener("HEHE", callback)
self:dispatchEvent({name = "HEHE", x = 5, y = 10, touchInTarget = true})