user1995781
user1995781

Reputation: 19453

Actionscript dispatch custom event with information

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.

Upvotes: 0

Views: 80

Answers (3)

tensorcrow
tensorcrow

Reputation: 68

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.

Upvotes: 1

Subash Selvaraj
Subash Selvaraj

Reputation: 3385

  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.

Upvotes: 0

Black Dynamite
Black Dynamite

Reputation: 4147

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.

Upvotes: 0

Related Questions