User Guide
Event listeners 359
When a user interacts with the resulting SWF file by clicking on the square, Flash Player
generates the following trace output:
clickHandler detected an event of type: click
the this keyword refers to: [object global]
Notice that the event object is passed as an argument to clickHandler(). This allows your
listener function to examine the event object. In this example, you use the event object's
type
property to ascertain that the event is a click event.
The example also checks the value of the
this keyword. In this case, this represents the
global object, which makes sense because the function is defined outside of any custom class
or object.
Listener function defined as a class method
The following example is identical to the previous example that defines the ClickExample
class except that the
clickHandler() function is defined as a method of the ChildSprite
class:
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);
}
private function clickHandler(event:MouseEvent):void
{
trace("clickHandler detected an event of type: " + event.type);