FMS服务器端最简单的EventDispatcher类 转
参考了as2时代的GEventDispatcher类,改写成SSAS,并且精简了一下只提供3个最基本的功能
1. addEventListener( eventType:String , listener:object , listenerHandler:String )
2. removeEventListener(eventType:String , listener:object , listenerHandler:String )
3. dispatchEvent({type:"type"})
演示代码:
N2EventDispatcher 源代码:
1. addEventListener( eventType:String , listener:object , listenerHandler:String )
2. removeEventListener(eventType:String , listener:object , listenerHandler:String )
3. dispatchEvent({type:"type"})
演示代码:
//需要用到的2个类
AAA = function(){}
BBB = function(){}
//首先加载EventDispatcher
load("N2EventDispatcher.asc")
application.onAppStart = function(){
//a1是AAA类的一个实例
var a1 = new AAA()
//把a1变成广播者后,a1就可以广播事件了
N2EventDispatcher.init(a1)
//b1是BBB类的一个实例 ,他是一个监听者,监听a1广播的test事件
var b1 = new BBB() //listener
b1.onTest= function(eo /* :Event */){
trace("收到"+eo.type +"事件")
}
a1.addEventListener("test",b1,"onTest") //参数为 (eventType , 监听者,监听者的handler)
a1.dispatchEvent({type:"test"}) //广播事件,相当于 a1.dispatchEvent(new Event("test"))
a1.removeEventListener("test",b1,"onTest") //删除监听
a1.dispatchEvent({type:"test"}) //再广播就不会触发了
}
AAA = function(){}
BBB = function(){}
//首先加载EventDispatcher
load("N2EventDispatcher.asc")
application.onAppStart = function(){
//a1是AAA类的一个实例
var a1 = new AAA()
//把a1变成广播者后,a1就可以广播事件了
N2EventDispatcher.init(a1)
//b1是BBB类的一个实例 ,他是一个监听者,监听a1广播的test事件
var b1 = new BBB() //listener
b1.onTest= function(eo /* :Event */){
trace("收到"+eo.type +"事件")
}
a1.addEventListener("test",b1,"onTest") //参数为 (eventType , 监听者,监听者的handler)
a1.dispatchEvent({type:"test"}) //广播事件,相当于 a1.dispatchEvent(new Event("test"))
a1.removeEventListener("test",b1,"onTest") //删除监听
a1.dispatchEvent({type:"test"}) //再广播就不会触发了
}
N2EventDispatcher 源代码:
try { var dummy = N2EventDispatcher; } catch ( e ) { // #ifndef N2VideoPlayback
N2EventDispatcher = function(){}
N2EventDispatcher.instance = undefined
N2EventDispatcher.prototype._listeners = undefined
N2EventDispatcher.prototype.init = function(p_obj)
{
if (N2EventDispatcher.instance == undefined)
{
N2EventDispatcher.instance = new N2EventDispatcher()
}
p_obj.dispatchEvent = N2EventDispatcher.instance.dispatchEvent;
p_obj.addEventListener = N2EventDispatcher.instance.addEventListener;
p_obj.removeEventListener = N2EventDispatcher.instance.removeEventListener;
}
N2EventDispatcher.prototype.addEventListener = function(p_eventType/* :String */,p_obj /*:Object*/,p_function/*String*/)
{
if (this._listeners == undefined)
{
this._listeners = {};
}
var listeners/* :Array */ = this._listeners[p_eventType];
if (listeners == undefined)
{
this._listeners[p_eventType] = listeners = [];
}
if (N2EventDispatcher.indexOfListener(listeners,p_obj,p_function) == -1)
{
listeners.push({o:p_obj,f:p_function});
}
}
N2EventDispatcher.prototype.removeEventListener = function (p_eventType /*:String*/,p_obj /*:Object*/,p_function /*:String*/) /*:Void*/
{
var listeners = this._listeners[p_eventType];
if (listeners == undefined) { return; }
var index /*:Number*/ = N2EventDispatcher.indexOfListener(listeners,p_obj,p_function);
if (index != -1) { listeners.splice(index,1); }
}
N2EventDispatcher.prototype.dispatchEvent = function(p_eventObj)
{
if(this._listeners == undefined) return
if (p_eventObj.target == undefined) { p_eventObj.target = this; }
var arr = this._listeners[p_eventObj.type]
if (arr != undefined)
{
for( i in arr){
var scrope = arr[i].o
var fun = arr[i].f
scrope[fun](p_eventObj)
}
}
}
N2EventDispatcher.indexOfListener = function(p_listeners /*:Array*/ , p_obj/*:Object*/,p_function /*:String*/) /*:Number*/
{
var l /*:Number*/ = p_listeners.length;
var i /*:Number*/ = -1;
while (++i < l) {
var obj /*:Object*/ = p_listeners[i];
if (obj.o == p_obj && obj.f == p_function) { return i; }
}
return -1;
}
N2EventDispatcher.init = N2EventDispatcher.prototype.init
}
N2EventDispatcher = function(){}
N2EventDispatcher.instance = undefined
N2EventDispatcher.prototype._listeners = undefined
N2EventDispatcher.prototype.init = function(p_obj)
{
if (N2EventDispatcher.instance == undefined)
{
N2EventDispatcher.instance = new N2EventDispatcher()
}
p_obj.dispatchEvent = N2EventDispatcher.instance.dispatchEvent;
p_obj.addEventListener = N2EventDispatcher.instance.addEventListener;
p_obj.removeEventListener = N2EventDispatcher.instance.removeEventListener;
}
N2EventDispatcher.prototype.addEventListener = function(p_eventType/* :String */,p_obj /*:Object*/,p_function/*String*/)
{
if (this._listeners == undefined)
{
this._listeners = {};
}
var listeners/* :Array */ = this._listeners[p_eventType];
if (listeners == undefined)
{
this._listeners[p_eventType] = listeners = [];
}
if (N2EventDispatcher.indexOfListener(listeners,p_obj,p_function) == -1)
{
listeners.push({o:p_obj,f:p_function});
}
}
N2EventDispatcher.prototype.removeEventListener = function (p_eventType /*:String*/,p_obj /*:Object*/,p_function /*:String*/) /*:Void*/
{
var listeners = this._listeners[p_eventType];
if (listeners == undefined) { return; }
var index /*:Number*/ = N2EventDispatcher.indexOfListener(listeners,p_obj,p_function);
if (index != -1) { listeners.splice(index,1); }
}
N2EventDispatcher.prototype.dispatchEvent = function(p_eventObj)
{
if(this._listeners == undefined) return
if (p_eventObj.target == undefined) { p_eventObj.target = this; }
var arr = this._listeners[p_eventObj.type]
if (arr != undefined)
{
for( i in arr){
var scrope = arr[i].o
var fun = arr[i].f
scrope[fun](p_eventObj)
}
}
}
N2EventDispatcher.indexOfListener = function(p_listeners /*:Array*/ , p_obj/*:Object*/,p_function /*:String*/) /*:Number*/
{
var l /*:Number*/ = p_listeners.length;
var i /*:Number*/ = -1;
while (++i < l) {
var obj /*:Object*/ = p_listeners[i];
if (obj.o == p_obj && obj.f == p_function) { return i; }
}
return -1;
}
N2EventDispatcher.init = N2EventDispatcher.prototype.init
}
事实上,没有什么要比发现、培养、呵护、调整自己的心智的力量更重要的事情了.........