This content has been marked as final. Show 2 replies
The hole concept of event dispatcher is that events get fired all the time, if there is someone listening to the event or not. In your own example, the CLICK event gets fired each time someone clicks on the object.
When you add an event listener basically what you are doing is adding a function to a queue of functions that get executed each time a specific event is fired. This is very important to keep in mind from the object disposal point of view, since for the garbage collector to actually kill an object all reference to it must be eliminated, and this include the reference in this queue for the listening function.
The reason that an event name is simply a String is so you can actually define any event you might like. Still is good practice to define constants for your events names since it will avoid a lot of errors when writing code.
Now to fire an event all you need to do is dispatch a specific event using the "dispatchEvent" function. This is what happens each time you click on an object, the dispatchEvent(new Event(Event.CLICK))) function is called.
If you are using custom events it's a good idea to extend the Event class so you can add some extra information to the event, for example, in the MouseEvent the x and y coordinates of the mouse are added. To dispatch a custom event all you do is call the event dispatcher with the custom event:
dispatchEvent(new CustomEventClass(Some Data));