您好,登錄后才能下訂單哦!
驗證:誰發送,誰接收
Event :
module demo{
export class myEvent extends egret.Event{
public static readonly _DEMO_EVENT : string = "_demo_event";
private _data : ImyEventName = null;
public constructor( $ty : string , $data : ImyEventName ){
super( $ty , false , false );
this._data = $data;
}
public get Data() : ImyEventName{
return this._data;
}
}
export interface ImyEventName{
/**發送者*/
_sender : string;
_dispatcher : myDispacherEvent;
}
}
EventDispatcher:
module demo{
export class myDispacherEvent extends egret.EventDispatcher{
/**
* 發送事件
* @param {string} $ty
* @param {string} $myName
*/
public sender( $ty : string , $myName : string ) : void{
let $event : myEvent = new myEvent(
$ty,
{
_sender : $myName,
_dispatcher : this
}
);
this.dispatchEvent( $event );
}
}
}
測試代碼:
private $dispatcher_0 : demo.myDispacherEvent = null;
private $dispatcher_1 : demo.myDispacherEvent = null;
/**
* preload資源組加載完成
* Preload resource group is loaded
*/
private onResourceLoadComplete(event: RES.ResourceEvent) {
if (event.groupName == "preload") {
this.stage.removeChild(this.loadingView);
RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
// this.createGameScene();
this.$dispatcher_0 = new demo.myDispacherEvent();
this.$dispatcher_0.addEventListener( demo.myEvent._DEMO_EVENT , this.onDemo , this );
this.$dispatcher_1 = new demo.myDispacherEvent();
this.$dispatcher_1.addEventListener( demo.myEvent._DEMO_EVENT , this.onDemo , this );
this.$dispatcher_0.sender( myEvent._DEMO_EVENT , "Aonaufly" );
this.$dispatcher_1.sender( myEvent._DEMO_EVENT , "Ainy" );
}
}
private onDemo( $e : demo.myEvent ) : void{
if( $e.type == demo.myEvent._DEMO_EVENT ){
egret.log(`發送者:${$e.Data._sender}, dispatcher_0 : ${ $e.Data._dispatcher == this.$dispatcher_0 }`);
}
}
結果:
Aonaufly 發送的 , Ainy是不會觸發的 , 切記。。。。。。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。