该命题应用场景想用的自然有。废话不表,直接上代码片断
首先定义自定义事件回调函数:
local function CustomCallBack(event)
printf("Test Custom Event")
end
注册自定义事件:
local listenerCustom=cc.EventListenerCustom:create("Test_Event",CustomCallBack)
local customEventDispatch=cc.Director:getInstance():getEventDispatcher()
customEventDispatch:addEventListenerWithFixedPriority(listenerCustom, 1)
LUA中发送自定义事件:
local myEvent=cc.EventCustom:new("Test_Event")
customEventDispatch:dispatchEvent(myEvent)
也可以在C++中发送自定义事件,lua中同样会响应:
cocos2d::Director::getInstance()->getEventDispatcher()->dispatchCustomEvent("Test_Event");