- Home
- Tutorials
- Flash
- Intermediate
- The Flash MX Event Model

Page 3 of 3
Guy Watson
This user is yet to take control of their account and provide a biography. If you are the author of this article, please contact us via support AT actionscript DOT org.
View all articles by Guy WatsonMovieclips and Buttons listen automatically:
As mentioned previously, for an object to receive event notification it has to be registered with the predefined actionscript object that provides those events. Since Flash 5, Movieclips were objects that inherited from the Movieclip class, now, in Flash MX, Buttons are objects that inherit from the Button class. All instances of the Moveclip class have their interest registered with the Movieclip news broadcast automatically and all instances of the Button class have their interest registered with the Button news broadcast automatically. This means that you do not need to explicity use the addListener method, for a Movieclip to be notified of any Movieclip events, and the same goes for Buttons. So it is possible to do the following:
//create a new movieclip on the _root timeline
_root.createEmptyMovieClip("mymovieclip", 1);
//define the ‘onMouseMove’ event handler for our Movieclip
_root.mymovieclip.onMouseMove = function() {
//move this movieclip to the mouses position
this._x = _root._xmouse;
this._y = _root._ymouse;
};
So lets explain what happens now when the the user moves his mouse in actionscript terms. Whenever the user moves his mouse the Movieclip object named ‘mymovieclip’ is notifed that the onMouseMove event has occurred and then the onMouseMove event handler is triggered, which in turn executes the code that moves the movieclip to the mouses position.
All Movieclips automatically receive notification of the Key, Stage and Mouse events automatically.
Flash 5 Event Model:
Flash 5 introduced a basic event model for movieclips and button, this involved the use of the onClipEvent event handler. So in the last example, we moved a movieclip to the mouses position whenver the mouse was moved, if we wanted to achieve the same thing using the Flash 5 Event model, we would place the following code on the outside of the movieclip, we wanted to move:
onClipEvent (mouseMove) {
this._x = _root._xmouse;
this._y = _root._ymouse;
}
Or if we wanted to run some code whenever our button was pressed we would place the following code on the outside of our button:
on (press) {
trace("Our button was pressed");
}
The above code can now be written in Flash MX, using the new event model as follows:
Yourbutton.onPress = function() {
trace("Our button was pressed");
};
You can still use the old Flash 5 Event model in Flash MX for the sake of backwards compatibility, it is not officially deprecated by Macromedia, but in my opinion you should consider it to be. There are absolutely no advantages that can be gained from using the old event model.
Flem is no longer needed in Flash MX:
Branden Hall created an event model in the form of an actionscript exstension named Flem in the early days of Flash 5. This gave you the same capabilities that the in built Flash MX Event model now provides therefore, the usage of this library is no longer required.
Advantages of using the new event model:
The first thing that you gain by using the Flash MX event model as opposed to the old event model is that you can change your event handlers at any time, thus changing the function that is executed when an event occurs. Also, because an event handler is now just a property of an object, it can also be deleted thus saving cpu usage, which could not be achieved without a bit of trickey once the movie was running with Flash 5. In flash 5 you couldn’t attach a movieclip from the library using the attachmovie method and then assign an event handler to it because the onClipEvent() event handler had to be defined on the movieclip as opposed to inside of it, at authoring time. It is now very simple to achieve that in Flash MX. Finally the new Flash MX event model is a lot cleaner, easier to use and more organised than the old event model that Flash 5 provided.
Editor's note: You can find supplementary additions and comments regarding this tutorial on the original post at FlashGuru's MX 101. Thanks heaps to FlashGuru for this top quality tute!

