Event Handlers
Not every event triggers the execution of code. Events regularly occur without affecting a movie. A user may, for example, generate dozens of events by clicking repeatedly on a button, but those clicks may be ignored. Why? Because, on their own, events can't cause code to execute -- we must write code to react to events explicitly. To instruct the interpreter to execute some code in response to an event, we add a so-called event handler that describes the action to take when the specified event occurs. Event handlers are so named because they catch, or handle, the events in a movie.
An event handler is akin to a specially named function that is automatically invoked when a particular event occurs. Creating an event handler is, hence, very much like creating a function, with a few twists:
- Event handlers have predetermined names such as keyDown. You can't name an event handler whatever you like; you have to use the predefined names shown later in Table 10-1 and Table 10-2.
- Event handlers are not declared with the function statement.
- Event handlers must be attached to buttons, movie clips, or objects, not frames.
WARNING
Most events were first introduced in Flash 5. If exporting to Flash 4 format, use only the button event handlers (only button events were supported in Flash 4), and test your work carefully in the Flash 4 Player.