Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to pass parameters through dispatchEvent.

Explorer ,
Feb 04, 2012 Feb 04, 2012

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

TOPICS
ActionScript
5.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 04, 2012 Feb 04, 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():Eve

...
Translate
LEGEND ,
Feb 04, 2012 Feb 04, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 05, 2012 Feb 05, 2012
LATEST

Hi,

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

Regards,

Sreelash

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines