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.

Chapter 31: Mouse input<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

Your application can create interactivity by capturing and responding to mouse input. Note that this section assumes<br />

that you are already familiar with the ActionScript 3.0 ev<strong>en</strong>t model. For more information, see “Handling ev<strong>en</strong>ts” on<br />

page 125.<br />

For information on discovering what kind of mouse support is available (such as persist<strong>en</strong>t cursor, stylus or touch<br />

input) during runtime, see “Discovering input types” on page 557.<br />

More Help topics<br />

flash.ui.Mouse<br />

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

“Touch, multitouch and gesture input” on page 580<br />

Capturing mouse input<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

Mouse clicks create mouse ev<strong>en</strong>ts that can be used to trigger interactive functionality. You can add an ev<strong>en</strong>t list<strong>en</strong>er to<br />

the Stage to list<strong>en</strong> for mouse ev<strong>en</strong>ts that occur anywhere within the SWF file. You can also add ev<strong>en</strong>t list<strong>en</strong>ers to objects<br />

on the Stage that inherit from InteractiveObject (for example, Sprite or MovieClip); these list<strong>en</strong>ers are triggered wh<strong>en</strong><br />

the object is clicked.<br />

As with keyboard ev<strong>en</strong>ts, mouse ev<strong>en</strong>ts bubble. In the following example, because square is a child of the Stage, the<br />

ev<strong>en</strong>t dispatches both from the sprite square as well as from the Stage object wh<strong>en</strong> the square is clicked:<br />

var square:Sprite = new Sprite();<br />

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

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

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

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

square.x =<br />

square.y = 50;<br />

addChild(square);<br />

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

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

{<br />

trace(ev<strong>en</strong>t.curr<strong>en</strong>tTarget.toString() + " dispatches MouseEv<strong>en</strong>t. Local coords [" +<br />

ev<strong>en</strong>t.localX + "," + ev<strong>en</strong>t.localY + "] Stage coords [" + ev<strong>en</strong>t.stageX + "," + ev<strong>en</strong>t.stageY + "]");<br />

}<br />

Last updated 6/6/2012<br />

574

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

Saved successfully!

Ooh no, something went wrong!