How to listen for both tap and gestures at the same time in Adobe AIR for iOS?
Asked Answered
M

3

2

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.

Medlock answered 2/8, 2012 at 0:17 Comment(0)
A
1

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.

Ardine answered 2/8, 2012 at 6:24 Comment(4)
wouldn't using Mouse events for touch be a problem? Maybe they will remove the mouse functionality simualtion on tapping in the future, hence the dedicated event for touchSchoolboy
Who do you mean with "they"? Flex is an Apache project, and I don't think the community will decide to make the Flex mobile components to work with touch events only.Spry
Ok sorry I thought it was managed by Adobe. I've now implemented with Mouse events, and using standard gestures it worked really well (and no performance impact in my case).Schoolboy
but wouldn't using standard Mouse Click events for "Tap" touch disable the ability for multiple simultaneous touches?Pebrook
G
3
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);
Guth answered 13/12, 2012 at 18:55 Comment(0)
A
1

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.

Ardine answered 2/8, 2012 at 6:24 Comment(4)
wouldn't using Mouse events for touch be a problem? Maybe they will remove the mouse functionality simualtion on tapping in the future, hence the dedicated event for touchSchoolboy
Who do you mean with "they"? Flex is an Apache project, and I don't think the community will decide to make the Flex mobile components to work with touch events only.Spry
Ok sorry I thought it was managed by Adobe. I've now implemented with Mouse events, and using standard gestures it worked really well (and no performance impact in my case).Schoolboy
but wouldn't using standard Mouse Click events for "Tap" touch disable the ability for multiple simultaneous touches?Pebrook
S
1

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.

Spry answered 2/8, 2012 at 6:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.