該命題應用場景想用的自然有。廢話不表,直接上代碼片斷
首先定義自定義事件回調函數:
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");