Help,Is there a CCNotificationCetner in jsb?


#1

I am using the cocos2d-x-2.2 jsb,but I can not find the CCNotificationCetner binding,should I write a customNotificationCenter by myself?


#2

You could just copy the implementation from cocos2d-html5, it’s pure JS implementation, No need to bind it.


#3

Thank u ,James, I have try to write a simple NotificationCenter,and when i find the real CCNotificationCenter,it’s looks like my Notification Center,haha,it’s a joke,Finally,i will use the CCNotifacationCenter.

var EventCenter = cc.Class.extend({

    m_ListenerList:null,


    ctor:function()
    {
        this.m_ListenerList = [];
    },
    dispatchEvent:function(event,data)
    {
        var len = this.m_ListenerList.length;
        if(!this.m_ListenerList || len == 0)
        {
            return;
        }
        var i = 0;
        for(; i < len; i++)
        {
            var item = this.m_ListenerList[i];
            if(item.getEvent() == event)
            {
                var callback = item.getCallbackFun();
                callback(data);
                break;
            }
        }
    },
    addEventListener:function(listener,callbackFun,event)
    {
        var item =  new EventListenerData(listener,callbackFun,event);
        this.m_ListenerList.push(item);
    },
    removeEventListener:function(listener,event)
    {
        var len = this.m_ListenerList.length;
        if(!this.m_ListenerList || len == 0)
        {
            return;
        }
        var i = 0;
        for(; i < len; i++)
        {
            var item = this.m_ListenerList[i];
            if(item.getEvent() == event && item.getListener() == listener)
            {
                this.m_ListenerList.splice(item,1);
                item.destory();
                item = null;
                break;
            }
        }
    },




});

var _instance_EventCenter = null;

EventCenter.GetInstance = function()
{
    if(!_instance_EventCenter)
    {
        _instance_EventCenter = new EventCenter();
    }
    return _instance_EventCenter;
};

var EventListenerData = cc.Class.extend({

   m_listener:null,
   m_callbackFun:null,
   m_event:null,
   ctor:function(listener,callbackFun,event)
   {
       this.m_listener = listener;
       this.m_callbackFun = callbackFun;
       this.m_event = event;

   },
   getListener:function()
   {
       return this.m_listener;
   },
   getCallbackFun:function()
   {
       return this.m_callbackFun;
   },
   getEvent:function()
   {
       return this.m_event;
   },
   destory:function()
   {
       this.m_listener = null;
       this.m_callbackFun = null;
       this.m_event = null;
   },

});