0

In actionscript, we can dispatch event with this:

dispatchEvent(new Event("MyEvent"));

And listen the event via this:

stage.addEventListener("MyEvent", MyFunc);

But how can I passing additional information with the event so that I can access it in MyFunc function?

Thank you.

3 Answers 3

1

You can use an example provided by Subash Selvaraj, this is a good example. My only point is: it seems to me that it's better to avoid creating a separate variable (for the event instance) every time you want to dispatch this event. You can add additional parameter to your class constructor instead. So your new event class may look like following:

import flash.events.Event;

public class MyEvent extends Event
{
    public var objEventData:Object;

    public function MyEvent(type:String, event_data:Object=null, bubbles:Boolean=false, cancelable:Boolean=false)
    {
         super(type, bubbles, cancelable);
         objEventData = event_data;
    }

    public override function clone():Event 
    {
        return new MyEvent(type, objEventData, bubbles, cancelable);
    } 

}

And in this case you can dispatch your event just like this:

dispatchEvent(new MyEvent(EVENT_TYPE, YOUR_DATA) );

After that you can access the passed data from your event handler, i.e. MyFunc:

private function MyFunc(event:MyEvent):void
{
    var buff:Object = event.objEventData;
}

You can replace an Object class with any type you need.

Sign up to request clarification or add additional context in comments.

Comments

0

You would have to make a custom event, and add custom properties to it. Here is an example.

    public class CustomEvent extends Event
    {
         public function CustomEvent (type:String, 
                                      bubbles:Boolean=true,          
                                      cancelable:Boolean=false)
         {
              super(type, bubbles, cancelable);
         }

         public var dataYouWantToPass:OfSomeClass;
     }

The bubbles=true depends on whether or not you want it to bubble up through the display list.

2 Comments

Thanks for your answer. But what is bubbles & cancelable mean? How can an event be cancelled?
@user1995781 By calling event.preventDefault();
0
  package {
        import flash.events.Event;    

        public class MyEvent extends Event {
            public static const TEST:String = "TEST";
            public myValue:Object = new Object();
            public function MyEvent (type:String, bubbles:Boolean=true, cancelable:Boolean=false) {
                // constructor code
                super(type, bubbles, cancelable);
            }
            public override function clone():Event
            {
                return new MyEvent(type, bubbles,cancelable);
            }

        }

    }

In your main file use like this,

private var eventInstance:MyEvent;
eventInstance= new MyEvent(MyEvent.TEST);
eventInstance.myValue = yourObject;
dispatchEvent(eventInstance);

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.