AS3 distribute and monitor events

The first step: a private event declare a static variable (uppercase)

package game.event
{
    public class MainEvent{     
        public static const RETURN_SETTING:String = "return_setting";
    }
}

Step two: the control layer (or business layer) to distribute event (event)

import game.event.MainEvent;
import laya.events.EventDispatcher;
···
public class MainController extends EventDispatcher
{
    static private var instance:MainController;
    static public function get Instance():MainController
    {
        if(instance == null){
            instance = new MainController();
        }
        return instance;
    }
。。。
       //派发事件
        this.event(MainEvent.RETURN_SETTING);
}

Note that objects are distributed MainController
instance -> Singleton understand: https://www.jianshu.com/p/018cb4e4565c
The third step: the view layer to bind to listen for events (on)

MainController.instance.on(MainEvent.RETURN_SETTING,this,onCloseAdvVideo);

Step four: the view layer unbundling listen for events (off)

MainController.instance.off(MainEvent.RETURN_SETTING,this,onCloseAdvVideo);

Guess you like

Origin blog.csdn.net/weixin_33827731/article/details/90964632