[cocos2dx]事件分发机制--lua源码(二)

[cocos2dx]事件分发机制--lua源码(二),第1张

概述使用过程 --加载组件cc(self):addComponent("components.behavior.EventProtocol"):exportMethods()--添加监听事件self:addEventListener("EVENT_PLAYER_DEAD", handler(self, self.changePlayerHead))--分发调用消息self:dispatc 使用过程
--加载组件cc(self):addComponent("components.behavior.EventProtocol"):exportMethods()--添加监听事件self:addEventListener("EVENT_PLAYER_DEAD",handler(self,self.changePlayerhead))--分发调用消息self:dispatchEvent({name = "EVENT_PLAYER_DEAD"})
下面来看看addEventListener(),dispatchEvent()重要函数的源码
event源码
--cc.components.behavior.EventProtocol.lualocal Component = import("..Component")local EventProtocol = class("EventProtocol",Component)function EventProtocol:ctor()    EventProtocol.super.ctor(self,"EventProtocol")    self.Listeners_ = {}    self.nextListenerHandleIndex_ = 0endfunction EventProtocol:addEventListener(eventname,Listener,tag)    assert(type(eventname) == "string" and eventname ~= "","EventProtocol:addEventListener() - invalID eventname")    eventname = string.upper(eventname)    if self.Listeners_[eventname] == nil then        self.Listeners_[eventname] = {}    end    local ttag = type(tag)    if ttag == "table" or ttag == "userdata" then        PRINT_DEPRECATED("EventProtocol:addEventListener(eventname,target) is deprecated,please use EventProtocol:addEventListener(eventname,handler(target,Listener),tag)")        Listener = handler(tag,Listener)        tag = ""    end    self.nextListenerHandleIndex_ = self.nextListenerHandleIndex_ + 1    local handle = tostring(self.nextListenerHandleIndex_)    tag = tag or ""    self.Listeners_[eventname][handle] = {Listener,tag}    if DEBUG > 1 then        printInfo("%s [EventProtocol] addEventListener() - event: %s,handle: %s,tag: %s",tostring(self.target_),eventname,handle,tostring(tag))    end    return handleendfunction EventProtocol:dispatchEvent(event)    event.name = string.upper(tostring(event.name))    local eventname = event.name    if DEBUG > 1 then        printInfo("%s [EventProtocol] dispatchEvent() - event %s",eventname)    end    if self.Listeners_[eventname] == nil then return end    event.target = self.target_    event.stop_ = false    event.stop = function(self)        self.stop_ = true    end    for handle,Listener in pairs(self.Listeners_[eventname]) do        if DEBUG > 1 then            printInfo("%s [EventProtocol] dispatchEvent() - dispatching event %s to Listener %s",handle)        end        -- Listener[1] = Listener        -- Listener[2] = tag        event.tag = Listener[2]        Listener[1](event)        if event.stop_ then            if DEBUG > 1 then                printInfo("%s [EventProtocol] dispatchEvent() - break dispatching for event %s",eventname)            end            break        end    end    return self.target_endfunction EventProtocol:removeEventListener(handletoRemove)    for eventname,ListenersForEvent in pairs(self.Listeners_) do        for handle,_ in pairs(ListenersForEvent) do            if handle == handletoRemove then                ListenersForEvent[handle] = nil                if DEBUG > 1 then                    printInfo("%s [EventProtocol] removeEventListener() - remove Listener [%s] for event %s",eventname)                end                return self.target_            end        end    end    return self.target_endfunction EventProtocol:removeEventListenersByTag(tagToRemove)    for eventname,Listener in pairs(ListenersForEvent) do            -- Listener[1] = Listener            -- Listener[2] = tag            if Listener[2] == tagToRemove then                ListenersForEvent[handle] = nil                if DEBUG > 1 then                    printInfo("%s [EventProtocol] removeEventListener() - remove Listener [%s] for event %s",eventname)                end            end        end    end    return self.target_endfunction EventProtocol:removeEventListenersByEvent(eventname)    self.Listeners_[string.upper(eventname)] = nil    if DEBUG > 1 then        printInfo("%s [EventProtocol] removeAllEventListenersForEvent() - remove all Listeners for event %s",eventname)    end    return self.target_endfunction EventProtocol:removeAllEventListeners()    self.Listeners_ = {}    if DEBUG > 1 then        printInfo("%s [EventProtocol] removeAllEventListeners() - remove all Listeners",tostring(self.target_))    end    return self.target_endfunction EventProtocol:hasEventListener(eventname)    eventname = string.upper(tostring(eventname))    local t = self.Listeners_[eventname]    if not t then        return false    end    for _,__ in pairs(t) do        return true    end    return falseendfunction EventProtocol:dumpAllEventListeners()    print("---- EventProtocol:dumpAllEventListeners() ----")    for name,Listeners in pairs(self.Listeners_) do        printf("-- event: %s",name)        for handle,Listener in pairs(Listeners) do            printf("--     Listener: %s,handle: %s",tostring(Listener[1]),tostring(handle))        end    end    return self.target_endfunction EventProtocol:exportMethods()    self:exportMethods_({        "addEventListener","dispatchEvent","removeEventListener","removeEventListenersByTag","removeEventListenersByEvent","removeAllEventListenersForEvent","removeAllEventListeners","hasEventListener","dumpAllEventListeners",})    return self.target_endfunction EventProtocol:onBind_()endfunction EventProtocol:onUnbind_()endreturn EventProtocol

从源码可以看出,

函数addEventListener(eventname,tag)作用就是把事件名和监听函数已key-value的形式保存到Listeners_表中,已便于后续的调用。 函数dispatchEvent(event)作用就是通过事件名的形式来调用监听函数。 事件分发机制的实质就是,把事件名以及监听事件名监听函数保存到一个容器中,然后通过事件名的形式来调用监听函数。

事件分发本质是采用的观察者模式: 注册观察者,事件产生并回调,取消注册。

[cocos2dx]事件分发机制(一)

总结

以上是内存溢出为你收集整理的[cocos2dx]事件分发机制--lua源码(二)全部内容,希望文章能够帮你解决[cocos2dx]事件分发机制--lua源码(二)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1066781.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存