ActionScript 3 AS3:单例事件控制器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 AS3:单例事件控制器相关的知识,希望对你有一定的参考价值。

-EventCentral.as-
package {

import flash.events.*;

    public class EventCentral extends EventDispatcher {
    private static var instance:EventCentral;
    public static function getInstance():EventCentral {
      if (instance == null){
         instance = new EventCentral(new SingletonBlocker());
      }
      return instance;
}
      public function EventCentral(blocker:SingletonBlocker):void{
        super();
        if (blocker == null) {
           throw new Error("Error: instantiation failed; Use EventCentral.getInstance()");
       }
}
        public override function dispatchEvent($event:Event):Boolean{
        return super.dispatchEvent($event);
}
}
}
internal class SingletonBlocker {}

--ProjectEvent.as--

package {
      import flash.events.Event
      public class ProjectEvent extends Event {
        public static const SOME_EVENT:String = "ProjectEvent.onSomeEvent";
        public var params:Object;
        public function ProjectEvent($type:String,$params:Object = null){
          super($type,true,true);
          this.params = $params;
}
       public override function clone():Event {
           return new ProjectEvent(this.type,this.params);
}
     override public function toString():String{
          return ("[Event ProjectEvent]");
}
}
}

To set up a listener:

EventCentral.getInstance().addEventListener('ProjectEvent.SOME_EVENT',handleSomeEvent);
function handleSomeEvent(event:ProjectEvent):void{
trace(event.params.param1);
}

to dispatch:

EventCentral.getInstance().dispatchEvent(new ProjectEvent('ProjectEvent.SOME_EVENT',{param1:'something'}));

以上是关于ActionScript 3 AS3:单例事件控制器的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 AS3 FlashDevelop新事件模板

ActionScript 3 AS3:onAdded事件监听器

ActionScript 3 AS3修订了自定义事件类

ActionScript 3 Flex AS3中的KeyPress /键盘事件处理

ActionScript 3 在AS3中使用自定义事件传递数据

ActionScript 3 鼠标事件使用AS3的简单绘图应用程序