Developing Android Applications with Adobe AIR [37]
The flash.ui.Multitouch class is a recent addition to the ActionScript language to support user input. The Android platform supports multitouch, but it is always a good practice to test the device’s capabilities:
import flash.ui.Multitouch;
if (Multitouch.supportsGestureEvents == true) {
trace(Multitouch.supportedGestures);
}
The supportedGestures property returns an array of gestures that your specific device understands. This is particularly important to test if you use unusual gestures. For instance, the list of gestures for the Nexus One is:
gestureZoom, gestureRotate, gesturePan, gestureSwipe, gestureTwoFingerTap
For a summary of gesture definitions across platforms, see the handy reference at http://www.lukew.com/ff/entry.asp?1071.
Testing support for multitouch is done separately:
if (Multitouch.supportsTouchEvents == true) {
trace(Multitouch.maxTouchPoints);
}
The maxTouchPoints property returns the number of touches supported simultaneously. Most Android devices currently support two points. We will come back to this in the section titled The TouchEvent Class.
The flash.ui.MultitouchInputMode class provides the mechanism to select the mode of input the application uses. Only one mode can be active at a time.
When using the mode NONE, all events are interpreted as mouseEvent. This is the default mode:
import flash.ui.MultitouchInputMode;
Multitouch.inputMode = MultitouchInputMode.NONE
With the GESTURE mode, all events are interpreted as GestureEvent:
Multitouch.inputMode = MultitouchInputMode.GESTURE;
With the TOUCH_POINT mode, all events are interpreted as touchEvent:
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT
The mode can be changed at runtime, but you should use it only if you have a case that requires it and if it is not confusing to the user. Only one mode, the last one chosen, is possible at a time.
The GestureEvent Class
A GestureEvent is the interpretation of multiple points as a recognizable pattern. The Flash platform offers three gesture classes: GestureEvent, TransformGestureEvent, and PressAndTapGestureEvent. Gestures cannot be detected in sequence. The user must finish the first gesture, lift her fingers, and then start the next gesture.
Here is how to set a listener for the event type you want to receive:
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_ZOOM, onZoom);
Gesture events have a phase property that is used to indicate the progress of the gesture. Its value is BEGIN when the finger is first pressed down; UPDATE while the finger is moving; and END when the finger leaves the screen. Another phase, ALL, is for events such as swipes or two-finger taps, which only return one phase.
A typical use for the phase property is to play one sound when the gesture begins and another sound when it ends:
import flash.ui.MultitouchInputMode;
import flash.events.GesturePhase;
import flash.events.TransformGestureEvent
function onZoom(event:TransformGestureEvent):void {
if (event.phase == GesturePhase.BEGIN) {
// play hello sound
} else if (event.phase == GesturePhase.END) {
// play good bye sound
}
}
Gesture events have other properties related to position, as well as some that are relevant to their particular type. One gesture event, TransformGestureEvent, has many types, which we will discuss in the following subsections.
NOTE
All of the illustrations in the following subsections courtesy of GestureWorks (http://www.gestureworks.com).
The Zoom Gesture
The zoom gesture is also referred to as pinching. With this gesture, the user places two fingers on the object, increasing and decreasing the distance between the fingers to scale the object up and down in size (see Figure 7-1).
Figure 7-1. The zoom gesture
The following code creates a sprite and scales it according to the movement being performed:
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.display.Sprite;
import flash.events.TransformGestureEvent;