9 Replies Latest reply on Oct 1, 2008 5:56 AM by Peter Lorent

    Dispatched Event not being caught

    eexposito
      I have the following custom event:

      package
      {
      import flash.events.Event;

      public class TestCustomEvent extends Event
      {
      public static const EVENT_NAME:String = "TestCustom";
      public var _message:String;

      public function TestCustomEvent(message:String)
      {
      super(EVENT_NAME,true,false);
      this._message = message;

      }

      public override function clone():Event
      {
      return new TestCustomEvent(_message);
      }

      public override function toString():String
      {
      return formatToString("TestCustomEvent", "type", "bubbles", "cancelable", "eventPhase", "_message");
      }
      }
      }

      which I am trying to dispatch from the following class:

      package
      {
      import flash.events.EventDispatcher;
      import flash.events.IEventDispatcher;
      import flash.events.Event;

      public class TestDispatch implements IEventDispatcher
      {
      private var dispatcher:EventDispatcher;

      public function TestDispatch()
      {
      dispatcher = new EventDispatcher(this);
      }

      public function RaiseEvent():void
      {
      var ev:TestCustomEvent = new TestCustomEvent("catch this");
      this.dispatchEvent(ev);
      }

      public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void{
      dispatcher.addEventListener(type, listener, useCapture, priority);
      }

      public function dispatchEvent(evt:Event):Boolean{
      return dispatcher.dispatchEvent(evt);
      }

      public function hasEventListener(type:String):Boolean{
      return dispatcher.hasEventListener(type);
      }

      public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void{
      dispatcher.removeEventListener(type, listener, useCapture);
      }

      public function willTrigger(type:String):Boolean {
      return dispatcher.willTrigger(type);
      }

      }
      }

      but in my application the event doesn't get caught:

      <?xml version="1.0"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init()" initialize="addListeners()">
      <mx:Script>
      <![CDATA[
      private function addListeners():void
      {
      this.addEventListener( TestCustomEvent.EVENT_NAME, onEventCaught );
      }
      private function init():void
      {
      var t:TestDispatch = new TestDispatch();
      t.RaiseEvent();
      }

      public function onEventCaught(event:TestCustomEvent):void
      {
      trace("caught");
      }
      ]]>
      </mx:Script>
      </mx:Application>


      Not sure why event is not being caught.

      Thanks,
      EE