cocos2dx 使用lua自定義事件

      該命題應用場景想用的自然有。廢話不表,直接上代碼片斷   

首先定義自定義事件回調函數:

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");


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章