Last modified on 28 January 2013, at 12:50

Introduction to ActionScript 2.0/Event Handling

Introduction to ActionScript 2.0
Control Statements Event Handling Primitive Data Types

Key concepts:


  • Events
  • Event handlers
  • Event handler declarations
  • Key and Mouse classes
  • Listeners
  • Polling

An event is an unpredictable event; you never know when it will happen, although you do know how to respond to it. Many events are based on the input devices of the end user's computer, such as mouse clicks. Others may be concerning objects in the application. In this section, we'll learn how to deal with events.

What are event handlers?Edit

An event handler is a function that shows what to do when an event occurs.[1] It is declared as follows:

objectName.eventHandlerName = function(){
     Statements to execute when the event occurs;
}
Code Result
//Assume there's a MovieClip with the instance name 'apple' on the stage.
apple.onPress = function(){
     trace("That's one tasty apple, all right!");
}

(When the apple is pressed)
That's one tasty apple, all right!

Each class has its own events. Examples of events in the MovieClip class include onPress (when the mouse button is pressed while the cursor is above the MovieClip), onRelease (when the MovieClip is released while the cursor is above the MovieClip), onLoad (when the MovieClip is loaded), and so on. We will go into detail about these in the MovieClip chapter.

What are the Key and Mouse classes?Edit

The Key and Mouse classes are classes that contain only events, static methods and static properties. You cannot create instances of the two classes. (Static methods and properties will be covered briefly in the next chapter and in detail in the third section). The Key and Mouse classes contain a lot of event handlers which are, surprisingly enough, related to the keyboard and mouse respectively. For the rest of this chapter, we'll be using the following event handlers:

  • Mouse.onMouseDown (when the left mouse button is pressed)
  • Key.onKeyDown (when any key is pressed)

What are listeners?Edit

Since we can't create instances of the mouse and key classes, how the heck are we supposed to use the useful events inside? The answer: listeners.

Let's say we want an apple instance to shrink whenever the mouse is pressed anywhere on the stage. Here's the code for it:

Code Result
//Assume there's a MovieClip with the instance name 'apple' on the stage.
Mouse.addListener(apple);
apple.onMouseDown = function(){
     this._width -= 2;
     this._height -= 2;
}

In this example, apple is added to the Mouse class's list of listeners. When the mouse is pressed anywhere on the stage, a notification is sent to the apple to shrink it.

The strategy of using listeners is called callback notification.

What is polling?Edit

Polling refers to the computer checking repetitively to see if an event has occurred. In a way, event handling is also polling, although this polling strategy is built into ActionScript. There's also a way to poll by yourself. This strategy, which serves as an alternative to event handling, will be covered in our detailed discussion of the Key class later on.

ConclusionEdit

Since we've now learnt all the basics of ActionScript, we can now look at each of the built-in classes up close and personal, and learn how to use each of them to create powerful and dynamic Flash applications.

NotesEdit

  1. Technically, an event handler is a property which contains a function.