Familiar with JS DOM events or Flash events, basic can immediately understand the use of Cc.eventmanager.
Cc.eventmanager has two ways of registering listeners, one of which is a native event, such as
Cc.eventManager.addListener ({ event:cc. Eventlistener.keyboard, function(keycode, event) { if (keycode = = cc. Key.back) { cc.director.end (); } this);
AddListener accepts an object. This is true for keyboard events, touch events, and so on. Often the structure of this object is cumbersome and each event is different.
In addition, customevent, for example, have been exposed to background and recovery events:
function(event) { Cc.log ("cc.game.event_hide!" ); }); // function(event) { Cc.log ("Cc.game.EVENT_SHOW"); });
These two events are relatively simple, similar to JS and Flash events, only need a simple string + a function.
The parameter event is such a structure:
Where UserData is the dispatch of the time.
The way to trigger custom events is also consistent with JS and Flash:
Cc.eventManager.dispatchCustomEvent ("Xxxxxevent", {a:1,b:2});
This dispatch will give you the content shown in the picture above.
But RemoveListener is a bit inconvenient. Supports all removal of custom events
function (Customeventname)
You cannot remove a single individual.
Look at the official example, seemingly basically do not remove listener, maybe JS a few object memory is not significant bar. If memory management is very sensitive, you may need to write one yourself.
/** * Created by Kenkozheng on 2014/8/20.*/varEventdispatcher =function() { This. Init ();};varp =Eventdispatcher.prototype;p._listenermap=NULL;p. Init=function(){ This. _listenermap =NewObject ();};/** * * @param event string* @param callback function*/P.addlistener=function(event, callback) {if(!callback | |!event)return; varListenerList = This. _listenermap[event]; if(!listenerlist) ListenerList= This. _listenermap[event] =NewArray (); for(vari = 0; i < listenerlist.length; i++) { if(Listenerlist[i] = =callback)return; } Listenerlist.push (callback);}; P.removelistener=function(event, callback) {if(!callback | |!event)return; varListenerList = This. _listenermap[event]; if(listenerlist) { for(vari = 0; i < listenerlist.length; i++) { if(Listenerlist[i] = =callback) {Listenerlist.splice (i,1); return; } } }};/** * * @param event String*/p.dispatchevent=function(event) {if( This. _listenermap[event]) { varListeners = This. _listenermap[event].slice (); for(vari = 0; i < listeners.length; i++) {listeners[i] (); } }}