13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Handling ev<strong>en</strong>ts<br />

List<strong>en</strong>er function defined outside of a class<br />

The following code creates a simple SWF file that displays a red square shape. A list<strong>en</strong>er function named<br />

clickHandler(), which is not part of a class, list<strong>en</strong>s for mouse click ev<strong>en</strong>ts on the red square.<br />

package<br />

{<br />

import flash.display.Sprite;<br />

}<br />

public class ClickExample ext<strong>en</strong>ds Sprite<br />

{<br />

public function ClickExample()<br />

{<br />

var child:ChildSprite = new ChildSprite();<br />

addChild(child);<br />

}<br />

}<br />

import flash.display.Sprite;<br />

import flash.ev<strong>en</strong>ts.MouseEv<strong>en</strong>t;<br />

class ChildSprite ext<strong>en</strong>ds Sprite<br />

{<br />

public function ChildSprite()<br />

{<br />

graphics.beginFill(0xFF0000);<br />

graphics.drawRect(0,0,100,100);<br />

graphics.<strong>en</strong>dFill();<br />

addEv<strong>en</strong>tList<strong>en</strong>er(MouseEv<strong>en</strong>t.CLICK, clickHandler);<br />

}<br />

}<br />

function clickHandler(ev<strong>en</strong>t:MouseEv<strong>en</strong>t):void<br />

{<br />

trace("clickHandler detected an ev<strong>en</strong>t of type: " + ev<strong>en</strong>t.type);<br />

trace("the this keyword refers to: " + this);<br />

}<br />

Wh<strong>en</strong> a user interacts with the resulting SWF file by clicking on the square, Flash Player or AIR g<strong>en</strong>erates the following<br />

trace output:<br />

clickHandler detected an ev<strong>en</strong>t of type: click<br />

the this keyword refers to: [object global]<br />

Notice that the ev<strong>en</strong>t object is passed as an argum<strong>en</strong>t to clickHandler(). This allows your list<strong>en</strong>er function to<br />

examine the ev<strong>en</strong>t object. In this example, you use the ev<strong>en</strong>t object's type property to ascertain that the ev<strong>en</strong>t is a click<br />

ev<strong>en</strong>t.<br />

The example also checks the value of the this keyword. In this case, this repres<strong>en</strong>ts the global object, which makes<br />

s<strong>en</strong>se because the function is defined outside of any custom class or object.<br />

List<strong>en</strong>er function defined as a class method<br />

The following example is id<strong>en</strong>tical to the previous example that defines the ClickExample class except that the<br />

clickHandler() function is defined as a method of the ChildSprite class:<br />

Last updated 6/6/2012<br />

135

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!