Skip to main content
Inspiring
February 4, 2012
Answered

How to pass parameters through dispatchEvent.

  • February 4, 2012
  • 1 reply
  • 5258 views

Hi,

                  I've written an imageLoader class. I want to dispatch an event after the completion of image loading to the document class from which imageLoader has been initialized.

public function onLoaderProgress(e:ProgressEvent) {

                              imgLoader.removeEventListener(ProgressEvent.PROGRESS, onLoaderProgress);

                          trace(e.bytesLoaded, e.bytesTotal);

                    }

                    public function onLoaderComplete(e:Event) {

                              imgLoader.removeEventListener(Event.COMPLETE, onLoaderComplete);

                              dispatchEvent(new Event("Image Loaded"));

                         imgContainer.addChild(imgLoader);

                    }

onLoaderComplete is the image loading complete event and onLoaderProgress is the image loading progress event. Here i have dispatched an event in inLoaderComplete event. I want to send parameters along with the dispatchEvent. How it can be done?

Regards,

Sreelash

This topic has been closed for replies.
Correct answer Ned Murphy

You can use the CustomeEvent class below to have parameters attached to your dispatchEvent function....

package

{

     import flash.events.Event;

     public class CustomEvent extends Event

     {

          public var params:Object;

          public function CustomEvent(type:String, params:Object, bubbles:Boolean = false, cancelable:Boolean = false)

          {

               super(type, bubbles, cancelable);

               this.params = params;

          }

          public override function clone():Event

          {

               return new CustomEvent(type, params, bubbles, cancelable);

          }

         

          public override function toString():String

          {

               return formatToString("CustomEvent", "params", "type", "bubbles", "cancelable", "eventPhase");

          }

         

     }

    

}

To dispatch the event you just use something like the following, where the parameters are specified as an object...

    dispatchEvent(new CustomEvent("ImageLoaded", { param1: someValue, otherParam: otherValue }, true));

At the receiving end in the ImageLoaded event handler you can acquire the parameters using...

     evt.params.param1

     evt.params.otherParam

where 'evt' is replaced with whatever name you assign to the argument passed into the event handler function

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
February 4, 2012

You can use the CustomeEvent class below to have parameters attached to your dispatchEvent function....

package

{

     import flash.events.Event;

     public class CustomEvent extends Event

     {

          public var params:Object;

          public function CustomEvent(type:String, params:Object, bubbles:Boolean = false, cancelable:Boolean = false)

          {

               super(type, bubbles, cancelable);

               this.params = params;

          }

          public override function clone():Event

          {

               return new CustomEvent(type, params, bubbles, cancelable);

          }

         

          public override function toString():String

          {

               return formatToString("CustomEvent", "params", "type", "bubbles", "cancelable", "eventPhase");

          }

         

     }

    

}

To dispatch the event you just use something like the following, where the parameters are specified as an object...

    dispatchEvent(new CustomEvent("ImageLoaded", { param1: someValue, otherParam: otherValue }, true));

At the receiving end in the ImageLoaded event handler you can acquire the parameters using...

     evt.params.param1

     evt.params.otherParam

where 'evt' is replaced with whatever name you assign to the argument passed into the event handler function

SreelashAuthor
Inspiring
February 6, 2012

Hi,

      Thank you very much. It got works. But i would like to know what's happening on each functions.

Regards,

Sreelash