AS3 Event Dispatcher Blog

There are 3 people involve in as3 event dispatcher.
1. The dispatcher
2. The listener
3. The worker

What are their job responsibilities?


The event dispatcher dispatches event.
In other words, it broadcast an event when it is triggered by
something. For instance, the basketball player throws the ball into
a goal, the goal dispatches event (+1 Score to Team A)

A dispatcher can dispatch a common event called Event, or
a Custom Event which you made. A custom event allows you
to insert one or more useful data into it. For example:
(1st Option) Point 1
(2nd Option) Points 3, Description: Ball is thrown from outer ring.

The dispatcher also needs to name the event that it is dispatching.
For instance, EnemyBall1, or Player13 or GoalTeamA.

A code example:
super.dispatchEvent (new CustomEvent (1,Ball.GOAL_TEAM_A) );

p.s. The Ball refers to class
While GOAL_TEAM_A is a public static const (constant variable)
with String data type. You can use any class public static const
string, not necessarily the public static const string from the
class that it is dispatching the event!


The event listener is the middle-man.
It interacts with both dispatcher and worker.
Since the dispatcher has already named his Custom Event with
a tag/name (such as BALL.GOAL_TEAM_A) it knows which worker
to call to perform a particular task when that particular Custom
Event is being dispatched from the object that he is listening.

Before it could know which Event is being called,
it must have an object to listen.
Example: you are listening to my blog (or rather reading my blog
than reading any other actionscript blog). When you notice
a word called BALL.GOAL_TEAM_A in my blog, you perform a task.
Such as copying it down to your note book. See how it works?
If other blog has the word called BALL.GOAL_TEAM_A, you do
not perform any task if you found it, because you are NOT
reading their blog.


The worker perform any task that you have described.

For a greater detail with codes , refer to this blog:

The dispatcher does 2 things during the dispatching event process. It adds a tag (unique identifier) to allow the Listener to know which event to look at, it uses a custom event to add more data to it so this custom event can acknowledge the listener AND provide useful data to the worker.
e.g. super.dispatchEvent(new CustomEvent(CustomEventType.EVENT_ID));

The listener needs to do things which are assigning the task to the worker when the object its listening to dispatches an event, the listener also needs to know the EVENT_ID so it knows which event it should be concern with. If the class its listening to have hundreds of event, with the EVENT_ID it can filter out all those events easily to see whether the event that its interested has been triggered.
triggerobject.addEventListener(CustomEventType.EVENT_ID, doSomething);

The worker needs to do 2 things, accept the custom event object that and do the work

public function doSomething (evt : CustomEvent) : void
{/*do something here */}


About ayumilove

I am Ayumilove, if you see some impersonators its not me.

One response to “AS3 Event Dispatcher Blog”

  1. ZREN says :

    Thanks, it’s very helpful for me. is very helpful for beginners.

%d bloggers like this: