Cocos Creator 自定义事件

抛出自定义事件

    var Custom_Event = new cc.Event.EventCustom("EventName", false)
    Custom_Event.setUserData(data)
    cc.find('UIScript').dispatchEvent(Custom_Event)

接收自定义事件

    var that = this
    cc.find('UIScript').on("EventName", function(event){
        var data = event.getUserData()
        cc.log(data)
    })

移除自定义事件: 
注意事项 : 
1.在creator中移除自定义事件必须和抛出同喜写法, 只有on和off的区别 !! 
2.自定义事件移除写到onDisable中, 在removeFromParent时会自动调用移除

cc.find('UIScript').on("EventName", this.customFun,this)
customFun: function(){
    cc.log("this is customEvent")
},
onDisable: function(){
    cc.find('UIScript').off("EventName", this.customFun,this)
}

posted on 2018-03-28 07:19  &大飞  阅读(895)  评论(0编辑  收藏  举报

导航