I am making an iOS game and I need a detection for both simple tapping and gestures (swipe etc) simultaneously. In AIR, I can only see one setting for the Multitouch input mode: TOUCH_POINT (which works for basic tapping) and GESTURE. But I need both at the same time so changing the mode isn't an option. How can I listen to both at the same time?
Thanks,
Can.
You can use standard mouse events for tap.
This would maintain gesture multitouch input mode.
Otherwise, Gestouch framework: NUI gestures detection framework for mouse, touch and multitouch AS3 development at GitHub might be of interest.
Also note performance impacts of touch / gesture event model:
Both touch and gesture input can be multitouch input depending on the
user’s device. ActionScript provides API for handling touch events,
gesture events, and individually tracked touch events for multitouch
input.
Note: Listening for touch and gesture events can consume a significant
amount of processing resources (equivalent to rendering several frames
per second), depending on the computing device and operating system.
It is often better to use mouse events when you do not actually need
the extra functionality provided by touch or gestures. When you do use
touch or gesture events, consider reducing the amount of graphical
changes that can occur, especially when such events can be dispatched
rapidly, as during a pan, rotate, or zoom operation. For example, you
could stop animation within a component while the user resizes it
using a zoom gesture.
import flash.events.EventDispatcher;
import flash.events.TouchEvent;
import flash.net.Responder;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
public class SwipeAndTap extends EventDispatcher
{
private var fingerX:int;
private var fingerY:int;
private var elem:Object;
public function SwipeAndTap(_elem:Object)
{
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
elem = _elem;
elem.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
elem.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
elem.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
}
private function onTouchBegin(e:TouchEvent):void
{
fingerX = e.stageX;
fingerY = e.stageY;
}
private function onTouchMove(e:TouchEvent):void
{
if(e.stageX > (fingerX+150) && (e.stageY > (fingerY-100) && e.stageY < (fingerY+100) ) )
{
// swipe right
dispatchEvent(new TouchSwipeRight(TouchSwipeRight.SWIPE_RIGHT, e));
}
else if(e.stageX < (fingerX-150) && (e.stageY > (fingerY-100) && e.stageY < (fingerY+100) ) )
{
// swipe left
dispatchEvent(new TouchSwipeLeft(TouchSwipeLeft.SWIPE_LEFT, e));
}
}
private function onTouchEnd(e:TouchEvent):void
{
// e.touchPointID;
if(e.stageX > (fingerX-40) && e.stageX < (fingerX+40))
{
dispatchEvent(new TouchEventTap(TouchEventTap.TAP, e));
elem.removeEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
elem.removeEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
elem.removeEventListener(TouchEvent.TOUCH_END, onTouchEnd);
}
}
}
Example:
var SAT:SwipeAndTap = new SwipeAndTap(stage);
SAT.addEventListener(TouchEventTap.TAP, LangSelected);
SAT.addEventListener(TouchSwipeRight.SWIPE_RIGHT, ENtoPL);
SAT.addEventListener(TouchSwipeLeft.SWIPE_LEFT, PLtoEN);
I'm not quite sure if you need to set TOUCH_POINT
for basic tapping. It should work just as well if you've got GESTURE
set. You can simulate it with mouse events.
At any rate, the default AIR gesture support isn't that good anyways, so it might not work that way, hence I'd recommend looking into the Gestouch library. You get much more sophisticated gesture support that work very well. I've been using it in my Flex/AS3 projects for many month now and I'm quite happy with it.