COCOS2D-JS Custom Event Monitoring Dispatch

Source: Internet
Author: User

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] (); }    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.