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

Touch, multitouch and gesture input<br />

Touch ev<strong>en</strong>t handling<br />

Flash Player 10.1 and later, Adobe AIR 2 and later<br />

Basic touch ev<strong>en</strong>ts are handled the same way you handle other ev<strong>en</strong>ts, like mouse ev<strong>en</strong>ts, in ActionScript. You can<br />

list<strong>en</strong> for a series of touch ev<strong>en</strong>ts defined by the ev<strong>en</strong>t type constants in the TouchEv<strong>en</strong>t class.<br />

Note: For multiple touch point input (such as touching a device with more than one finger), the first point of contact<br />

dispatches a mouse ev<strong>en</strong>t and a touch ev<strong>en</strong>t.<br />

To handle a basic touch ev<strong>en</strong>t:<br />

1 Set your application to handle touch ev<strong>en</strong>ts by setting the flash.ui.Multitouch.inputMode property to<br />

MultitouchInputMode.TOUCH_POINT.<br />

2 Attach an ev<strong>en</strong>t list<strong>en</strong>er to an instance of a class that inherits properties from the InteractiveObject class, such as<br />

Sprite or TextField.<br />

3 Specify the type of touch ev<strong>en</strong>t to handle.<br />

4 Call an ev<strong>en</strong>t handler function to do something in response to the ev<strong>en</strong>t.<br />

For example, the following code displays a message wh<strong>en</strong> the square drawn on mySprite is tapped on a touch-<strong>en</strong>abled<br />

scre<strong>en</strong>:<br />

Multitouch.inputMode=MultitouchInputMode.TOUCH_POINT;<br />

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

var myTextField:TextField = new TextField();<br />

mySprite.graphics.beginFill(0x336699);<br />

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

addChild(mySprite);<br />

mySprite.addEv<strong>en</strong>tList<strong>en</strong>er(TouchEv<strong>en</strong>t.TOUCH_TAP, taphandler);<br />

function taphandler(evt:TouchEv<strong>en</strong>t): void {<br />

myTextField.text = "I've be<strong>en</strong> tapped";<br />

myTextField.y = 50;<br />

addChild(myTextField);<br />

}<br />

Touch Ev<strong>en</strong>t properties<br />

Wh<strong>en</strong> an ev<strong>en</strong>t occurs, an ev<strong>en</strong>t object is created. The TouchEv<strong>en</strong>t object contains information about the location and<br />

conditions of the touch ev<strong>en</strong>t. You can use the properties of the ev<strong>en</strong>t object to retrieve that information.<br />

For example, the following code creates a TouchEv<strong>en</strong>t object evt, and th<strong>en</strong> displays the stageX property of the ev<strong>en</strong>t<br />

object (the x-coordinate of the point in the Stage space that the touch occurred) in the text field:<br />

Last updated 6/6/2012<br />

584

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

Saved successfully!

Ooh no, something went wrong!