User Guide
358 Handling Events
In the ActionScript 3.0 event model, there is no distinction between an event listener and a
listener function. ActionScript 3.0 does not have an EventListener interface, and listener
functions can be defined outside a class or as part of a class. Moreover, listener functions do
not have to be named
handleEvent()—they can be named with any valid identifier. In
ActionScript 3.0, you register the name of the actual listener function.
Listener function defined outside of a class
The following code creates a simple SWF file that displays a red square shape. A listener
function named
clickHandler(), which is not part of a class, listens for mouse click events
on the red square.
package
{
import flash.display.Sprite;
public class ClickExample extends Sprite
{
public function ClickExample()
{
var child:ChildSprite = new ChildSprite();
addChild(child);
}
}
}
import flash.display.Sprite;
import flash.events.MouseEvent;
class ChildSprite extends Sprite
{
public function ChildSprite()
{
graphics.beginFill(0xFF0000);
graphics.drawRect(0,0,100,100);
graphics.endFill();
addEventListener(MouseEvent.CLICK, clickHandler);
}
}
function clickHandler(event:MouseEvent):void
{
trace("clickHandler detected an event of type: " + event.type);
trace("the this keyword refers to: " + this);
}