flash event_model_history
The history
ActionScript 3 has a new event model which we will look into in this chapter. Before getting into detail, let’s remember the old event model which first came out in Flash 6.
Up until now we could define functions that we could pass as references to the listened event. Let’s take a simple example. To listen to the onRelease event of a button we needed the following code:
myButton.onRelease = function () { // display : _level0.myButton trace ( this ); this._alpha = 50; }
An anonymous function was defined on the onRelease property of the button. When a mouse click intervened on the button, Flash player executed the function that we had defined and reduced the opacity of the button by 50%. This caused us some problems.
Firstly, the function defined on the button could not be reused for another event on a different object. To resolve this problem of resusability, some developers used references to functions in order to manage the event:
function clickButton () { // display : _level0.myButton trace (this); this._alpha = 50; } myButton.onRelease = clickButton;
This allowed you to reuse the clickButton function for other events linked to different objects, making the code lighter in particular when listening to events in loops.
An important point to note is that the keyword this referred to the button here and not the timeline on which the clickButton was defined. The original execution context of the clickButton function was therefore lost as it was changed into a reference. The function embraced the context of the author object of the event.
The automatic reference translated by the use of the keyword thiswas the only way to make a reference to it. This behavior was somewhat disconcerting for a person discovering ActionScript. Here is an example making this ambiguity obvious:
function clickButton () { // display : _level0.myButton trace ( this ); this._alpha = 50; } myButton.onRelease = clickButton; clickButton();
When executing the clickButton function in the traditional way the keyword this made reference to the timeline in which it was defined in its original context.
The following code was fairly rigid:
var ref:MovieClip; for ( var i:Number = 0; i< 50; i++ ) { ref = this.attachTimeline ( "myClip", "myClip"+i, i ); ref.onRelease = function () { trace("clicked"); } }
Developers therefore preferred the use of a reusable separated function:
var ref:MovieClip; for ( var i:Number = 0; i< 50; i++ ) { ref = this.attachTimeline ( "myClip", "myClip"+i, i ); ref.onRelease = clickButton; } function clickButton ( ) { trace("clicked"); }
This mechanism was the same for most events. Macromedia at the time of Flash MX (Flash 6) realised the lack of suppleness of this system of event-defined functions and integrated the concept of listeners and broadcasters in Flash 6 player. The classes Key, Selection and Textfield were the first to use this idea of broadcast listeners. To listen to the information capture of a textfield we can use the following syntax:
var myListener:Object = new Object(); myListener.onChanged = function(pTextfield:TextField) { trace ( pTextfield._name + " has broadcast the event onChanged" ); }; myTextfield.addListener ( myListener );
By calling the addListener method on our textfield we subscribed a myListener object as listener of the onChanged object. A method of the same name had to be defined on the object listener in order for this to be executed when the event was broadcast.
This approach allowed you to subscribe an unlimited amount of listeners to an event, but also to send parameters to the listening method. These parameters were generally information linked to an event, for example the author of the diffused event.
In 2004, with the launch of Flash 7 player (Flash MX 2004), ActionScript 2 made its appearance as well as a load of components called V2 components. These extended the idea of listening broadcasters by integrating a addEvnetListener method to subscribe a listener to an event. Contrary to the addListener method, this method extended the idea of listening broadcasters by storing each listener in different internal tables for each event.
In looking at the evolution of ActionScript through the past years we can see the depth of work carried out by engineers aiming to integrate the idea of listeners in all new objects created. ActionScript 3 was developed with the goal of offering a standard and powerful language and extended this idea by integrating a new event model called Document Object Model (DOM3 Event Model) valid for all API objects in Flash 9 player. W3C defined the specification of this event model. You can see the latest revisions at the following address: http://www.w3.org/TR/DOM-Level-3-Events/
ActionScript 2 developers who have already used the EventDispatcher class will be delighted to know that all objects linked to Flash player directly inherit this class. We will therefore look at in depth the idea of listening broadcasters and see how to improve the clarity and flexibility of our code.
Worth remembering
- The old event model was entirely revisited in ActionScript 3.
- The power of ActionScript 3 resides in the native implementation of the EventDispatcher
Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42


