Categorised Events:

The Flash Players news bulletin is split into separate departments that deal with different events, these are known as objects, each of the predefined actionscript objects, such as Key, Mouse and Movieclip has its own news reporter that is experienced in watching out for events that relate to that object. So for example the Mouse object news reporter is only employed to notify the Mouse head office when the mouse is pressed, when the mouse is released and when the mouse is moved. When one of these events occur, the news reporter notifies the Mouse head office, which then in turn broadcasts a news bulletin.

The list of events that each departments news reporter is employed to look out for are listed in the actionscript dictionary.

Here is a list of some of the most commonly occurring events and their associated event handler:

Event Description / Event Handler

Mouse is moved/onMouseMove
Mouse is pressed/onMouseDown
Mouse is released/onMouseUp
Key is pressed/onKeyDown
Key is released/onKeyUp

So in terms of actionscript, each of the Predefined Objects, such as Mouse, Key and Movieclip has its own set of events that are related to that object. It wouldn’t make sense for the Key object to contain events relating to the Mouse and so on…..

Listeners:

Listeners are user defined actionscript objects that are registered with specific departments of the flash players news bulletin, because they are interested in seeing the news that those departments broadcast. After an object has registered interest with one of the news bulletin departments, whenever an event occurs, that the news bulletin department is looking out for, a news broadcast is sent out and the registered object will receive it.

So in actionsript terms, a listener is an object that contains event handlers, the object registers with a specific predefined object so that it can receive the events related to that predefined object.

Using the Listener Methods:

We have methods that allow us to register and unregister our interest in the broadcasts from the news bulletin departments. To register an objects interest with a news bulletin department we use the addListener method and to unregister our interest with a news bulletin department we use the removeListener method. Here is the process we go through to register our interest with a news bulletin departments broadcast, in this example our object is interested in the Mouse objects broadcast:

//create a new empty object
myobject = new Object();
//now register our interest with the Mouse objects broadcast
Mouse.addListener(myobject);

Here is the process that we would use to unregister our interest in the Mouse objects broadcast:

Mouse.removeListener(myobject);

So in actionsript terms, now, whenever a Mouse event occurs, the object named ‘myobject’ will be notified that a Mouse event occurred and which Mouse event occurred.

Adding event handlers to objects:

For the empty object we created named ‘myobject’ to act on the broadcasts it receives from the news bulletins Mouse department, we need to tell it what to do when a specific event occurs, so as you should now know, we have to define event handlers for the object. To define an event handler that will output the words "Mouse was moved" to the output window, whenever the mouse is moved, we would use the onMouseMove event handler, as follows:

//create a new empty object
myobject = new Object();
//now register our interest with the Mouse objects broadcast
Mouse.addListener(myobject);
//define the onMouseMove event handler for our object
myobject.onMouseMove = function() {
 trace("Mouse was moved");
};

Lets be clever and output the words "Key was pressed" to the output window, whenever a key is pressed, by registering our interest for the object above, with the Key news bulletin department and using the onKeyDown event handler:

//create a new empty object
myobject = new Object();
//now register our interest in the Mouse objects broadcast
Mouse.addListener(myobject);
//define the onMouseMove event handler for our object
myobject.onMouseMove = function() {
 trace("Mouse was moved");
};
//now register our interest in the Key objects broadcast
Key.addListener(myobject);
//define the onKeyDown event handler for our object
myobject.onKeyDown = function() {
 trace("Key was pressed");
};

So, lets explain what happens now when the user clicks his mouse in actionscript terms. Whenever the user clicks his mouse, the Mouse object notifies the object named ‘myobject’ that the onMouseDown event has occurred and then the onMouseDown event handler is triggered, which in turn runs the code that outputs the words "Mouse was moved" to the output window.

Continued overleaf...