1.
Laya
.
EventDispatcher是表示Laya引擎中API,需要创建一个父类接口,通过父类接口指向子类对象,当调用接口方法时,
会自动调用其子类
重写方法
export
interface
IEventDispatcher {
on(
type:
string,
caller:
any,
listener:
Function,
args:
Array<
any>):
Laya.
EventDispatcher;
once(
type:
string,
caller:
any,
listener:
Function,
args:
Array<
any>):
Laya.
EventDispatcher;
off(
type:
string,
caller:
any,
listener:
Function,
onceOnly:
boolean):
Laya.
EventDispatcher;
offAll(
type:
string):
Laya.
EventDispatcher;
event(
type:
string,
data:
any):
boolean;
hasListener(
type:
string):
boolean;
}
import {
IEventDispatcher }
from
"./IEventDispatcher";
export
class
Dispatcher {
private
static
_dspt:
IEventDispatcher =
new
Laya.
EventDispatcher();
constructor() {}
/**
* 侦听事件
*
@param
type
事件的类型
*
@param
caller
事件侦听函数的执行域(一般为this)
*
@param
listener
事件侦听函数
*
@param
args
(可选)事件侦听函数的回调参数
*/
public
static
on(
type:
string,
caller:
any,
listener:
Function,
args:
any[] =
null):
void {
Dispatcher.
_dspt.
on(
type,
caller,
listener,
args);
}
/**
* 移除侦听
*
@param
type
*
@param
caller
*
@param
listener
*
@param
onceOnly
*/
public
static
off(
type:
string,
caller:
any,
listener:
Function,
onceOnly:
boolean =
false):
void {
Dispatcher.
_dspt.
off(
type,
caller,
listener,
onceOnly);
}
/**
* 派发事件。
*
@param
type
事件类型。
*
@param
data
(可选)回调数据。<b>注意:</b>如果是需要传递多个参数 p1,p2,p3,...可以使用数组结构如:
[p1,p2,p3,...] ;如果需要回调单个参数 p ,且 p 是一个数组,则需要使用结构如:[p],其他的单个参数 p ,可以直接传入参数 p。
*
@return
此事件类型是否有侦听者,如果有侦听者则值为 true,否则值为 false。
*/
public
static
event(
type:
string,
data?:
any):
void {
Dispatcher.
_dspt.
event(
type,
data);
}
public
static
hasListener(
type:
string):
boolean {
return
Dispatcher.
_dspt.
hasListener(
type);
}
}