Dispatching events with parameters




Here i will show you how to give an event parameters

Navigation


Getting started

There are different kind of events like the MouseEvent or TimerEvent. You can also create  new events or dispatch them. Normaly this code is used:
addEventListener("your event", function)

dispatchEvent(new Event("your event"))


Making it

At first you need to create a new class. This class is a subclass of the Event class.
(See here for the Event class: Event Class)
package{
    import flash.events.Event
    public class My_Event extends Event{
        private var _data:*
        public function My_Event(type:String, __data:*, bubbles:Boolean=false, cancelable:Boolean=false){
            this._data = __data;
            super(type, bubbles, cancelable);
            return;
        }
        public function get data():*{
            return this._data;
        }
    }
}

Now you can give your event parameters. There is a short example how:
var data:Object = new Object();
data.variable1 = 1;
data.variable2 = 2;
var my_event:My_Event = new My_Event("hello", data);
dispatchEvent(my_event);

Now we want to read the value of the parameters:
private function test(event:My_Event):void{
    trace(event.data.variable1);
    trace(event.data.variable2);
    return;
}

Done, now you can give your events parameters. Here i showed it with only one  which contains all the values. The data is read only. You can change the value  of the single variables, but not the data.