温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Egret之自定义事件

发布时间:2020-06-30 07:18:18 来源:网络 阅读:1974 作者:Aonaufly 栏目:开发技术

验证:谁发送,谁接收
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 }`);
        }
    }

结果:
Egret之自定义事件

Aonaufly 发送的 , Ainy是不会触发的 , 切记。。。。。。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI