Tutorial details:
Written by: Guy Watson , MX 101
Time: 40 minutes
Difficulty Level: Intermediate
Requirements: Flash MX
Topics Covered: How to manage and monitor events in Flash MX.
Assumed knowledge: Variables, Pathing, Scripted Loops.

One of my favourite new features of Flash MX, has to be the new event model, it just makes life so much easier and makes coding so much more fun and so much more organised. If you dont know much about it, then now is the time to find out, you really are missing out, but no doubt you are already using it without even realising. This article will hopefully help everybody that reads it, whether you are a beginner or a flash super user, understand the flash event model, and help you to use it. I think the best way to explain something new to somebody so that they can learn from it is to compare the subject with something that humans can relate to in their everyday lifes; A real world Analogy, so this article uses analogies to convey some of the concepts...

What you need to know:

To understand some of the explanations and terms used in this article, you need to know certain things about flash, firstly you need to understand that flash is now Object Oriented and therefore you should already have a basic understanding of Object Oriented Programming with flash, if you can answer the questions below, you will can continue to read this article and hopefully you should understand everything, otherwise you should checkout this excellent site that gives an in-depth explanation of Object Oriented Programming for flash: Robin Debreiul.

What is an object?
What is a method?
What is a property?
What are the predefined objects in flash?
What is the difference between a function and a method?
What is the difference between a variable and a property?

The Event Model:

Most of us want to know what is going on in the world or in our local area, for that we look to newspapers, the television and the web to inform us what is happening. The news consists of a collection of ‘events’ that have occurred recently, they are labelled ‘events’ because they are important moments in time that other people want or need to know about.

Think of the Event Model in Flash to be the news bulletin, it is the flash players way of telling us that something important has occurred in a flash movie, the news that flash brings us isn’t of course as important as what goes on in our everyday lifes, but it can be very important to a script.

So in terms of Actionscript, the event model is the internal workings of the flash player that notify the flash movie when something important occurs.

An Event:

CNN for example, send out reporters to look for important news. When something happens in the world, such as a volcano erupting, that is labelled an event, it is something important then people may want or need to know about, so the news reporter that saw the volcano erupt quickly calls the head office and tells them to add this latest hot news story to the news bulletin, shortly following, the latest news story is then spread throughout the world for everybody to see via the means of television, the web, the radio and other such technologies.

The same thing happens with your flash movies but on a much smaller scale. The flash player has news reporters, when the user of a flash movie moves his mouse for example, a news reporter that spotted the mouse moving, quickly calls head office(the flash player) and asks them to add the latest breaking story to the news bulletin because the reporter knows that this is something that the objects of the flash movie may want to know, very shortly after(milli-seconds) the latest ‘event’ is broadcasted to the flash movie.

So in actionscript terms an event, is a nofitication that occurs in reponse to an action, a change in state; such as a key on the keyboard being pressed, the mouse being moved, or an xml document fully loading.

An Event Handler:

The flash players news bulletin shares the latest news with us so that we can act, meaning we can run a piece of code when we are told that an event has occurred. This is where Flash Event Handlers come into play.

An event handler is an instruction that you specify for a user defined object, that tells the object what to do when a specific event occurs.

So in actionscript terms an event handler is simply a property of any specific user defined object that contains a reference to an actionscript method. That method is called and the code inside that method is executed when the related event occurs.

Continued overleaf...