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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Handling ev<strong>en</strong>ts<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 />

private 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 />

}<br />

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

following trace output:<br />

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

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

Note that the this keyword refers to the ChildSprite instance named child. This is a change in behavior from<br />

ActionScript 2.0. If you used compon<strong>en</strong>ts in ActionScript 2.0, you may remember that wh<strong>en</strong> a class method was passed<br />

in to UIEv<strong>en</strong>tDispatcher.addEv<strong>en</strong>tList<strong>en</strong>er(), the scope of the method was bound to the compon<strong>en</strong>t that<br />

broadcast the ev<strong>en</strong>t instead of the class in which the list<strong>en</strong>er method was defined. In other words, if you used this<br />

technique in ActionScript 2.0, the this keyword would refer to the compon<strong>en</strong>t broadcasting the ev<strong>en</strong>t instead of the<br />

ChildSprite instance.<br />

This was a significant issue for some programmers because it meant that they could not access other methods and<br />

properties of the class containing the list<strong>en</strong>er method. As a workaround, ActionScript 2.0 programmers could use the<br />

mx.util.Delegate class to change the scope of the list<strong>en</strong>er method. This is no longer necessary, however, because<br />

ActionScript 3.0 creates a bound method wh<strong>en</strong> addEv<strong>en</strong>tList<strong>en</strong>er() is called. As a result, the this keyword refers<br />

to the ChildSprite instance named child, and the programmer has access to the other methods and properties of the<br />

ChildSprite class.<br />

Last updated 6/6/2012<br />

136

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

Saved successfully!

Ooh no, something went wrong!