Reprinted at http://www.cnblogs.com/kenkofox/p/3926797.html
If you are familiar with js’s dom event or flash event, you can basically understand cc.eventManager right away Usage.
cc.eventManager has two ways to register listeners, one is native events, for example
cc.eventManager.addListener({
event: cc.EventListener.KEYBOARD,
onKeyReleased: function(keyCode, event) {
if (keyCode == cc.KEY.back) {
cc.director.end();
}
}}, this );
addListener accepts an object. This is true for keyboard events, touch events, etc. The structure of this object is often troublesome, and each event is different.
The other is customEvent, such as background entry and recovery events that have been contacted:
//Enter the background
cc.eventManager.addCustomListener(cc.game.EVENT_HIDE, function(event){
cc.log("cc.game.EVENT_HIDE!");
});
//Resume display
cc.eventManager.addCustomListener(cc.game.EVENT_SHOW, function (event){
cc.log("cc.game.EVENT_SHOW");
});
These two events are relatively simple, similar to js and flash events, and only need a simple string + a function.
The parameter event has this structure:
where userData is dispatch Attached at the time.
Looking at the official example, it seems that the listener is basically not removed. Maybe the memory of several js objects is not significant. If memory management is very sensitive, you may need to write one yourself.
/**
* Created by kenkozheng on 2014/8/20.
*/
var EventDispatcher = function() {
this.init();
};
var p = EventDispatcher.prototype;
p._listenerMap = null;
p.init = function< span style="color: #000000">(){
this._listenerMap = new Object();
};
/**
*
* @param event String
* @param callback function
*/
p.addListener = function(event, callback){
if(!callback || !event)
return;
var listenerList = this._listenerMap[event];
< /span>if(!listenerList)
listenerList = this._listenerMap[event] = new Array();
for (var i = 0; i) { if span>(listenerList[i] == callback)
return;
}
listenerList.push(callback);
};
p.removeListener = function(event, callback){
if< /span>(!callback || !event)
return;
var listenerList = this ._listenerMap[event];
if(listenerList)
{
for (var i = 0; i) { if(listenerList[i] == callback)
{
listenerList.splice( i, 1);
return;
}
}
}
};
/**
*
* @param event String
*/
p.dispatchEvent = function(event){
if(this._listenerMap[event])
{
var listeners = this._listenerMap[event].slice();< br /> for (var i = 0; i) {
listeners[i]();
}
}
}
div>
cc.eventManager.addListener({
event: cc.EventListener.KEYBOARD,
onKeyReleased: span>function(keyCode, event) {
if (keyCode == cc.KEY.back) {
cc.director.end();
}
}}, this);
//Enter the background
cc.eventManager.addCustomListener(cc. game.EVENT_HIDE, function(event){
cc.log("cc. game.EVENT_HIDE!");
});
// Resume display
cc.eventManager.addCustomListener(cc.game.EVENT_SHOW, function< span style="color: #000000">(event){
cc.log("cc.game.EVENT_SHOW");
});
/**
* Created by kenkozheng on 2014/8/20.
*/
var EventDispatcher = function() {
this.init();
};
var p = EventDispatcher.prototype;
p._listenerMap = null;
p.init = function< span style="color: #000000">(){
this._listenerMap = new Object();
};
/**
*
* @param event String
* @param callback function
< span style="color: #008000">*/
p.addListener = function(event, callback){
if(!callback | | !event)
return ;
var listenerList = this. _listenerMap[event];
if(!listenerList)
listenerList < /span>= this._listenerMap[event] = new Array();
for (var span> i = 0; i) { if (listenerList[i] == callback)
return;
}
listenerList.push(callback);
};
p.removeListener = function(event, callback){< br /> if(!callback || !event)
return;
var span> listenerList = this._listenerMap[event];
if(listenerList)
{
for (var i = 0; i) {
< /span>if(listenerList[i] == callback)
{
listenerList. splice(i, 1);
return;
}
}
}
};
/**
*
* @param event String
*/
p.dispatchEvent = function(event){
if(this._listenerMap[event] )
{
var listeners = this._listenerMap[event].slice();
for (var i = 0; i) {
listeners[i]();
}
}
}
WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3657 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC