air/as3 : is there a way yto simulate TouchEvents?

1.1k Views Asked by At

Is there a way to simulate TouchEvent on AIR ?

I am developping a mobile game: TOUCHEvents work fine when I run the game on mobile.

I would like to test/debug on AIR : is there a way to test it on AIR ?

Regards

3

There are 3 best solutions below

0
On

You can try this approach, you can add others touch events support by listening MouseEvent.MOUSE_MOVE, MouseEvent.MOUSE_DOWN, etc :

package
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.events.TouchEvent;

    public class TestTouchEvent extends Sprite
    {
        public function TestTouchEvent()
        {
            stage.addEventListener( MouseEvent.CLICK, onClick );
            stage.addEventListener( TouchEvent.TOUCH_TAP, onTap );
        }

        protected function onClick( event : MouseEvent ):void
        {
            event.target.dispatchEvent( new TouchEvent( TouchEvent.TOUCH_TAP ) );
        }

        protected function onTap( event : TouchEvent ):void
        {
            trace("Tap event stuff");
        }
    }
}
1
On

You could addEventListener for both Touch and Mouse and point them to same function.

this.addEventListener("touchBegin", onSomethingDown, false, 0 true);
this.addEventListener("mouseDown", onSomethingDown, false, 0 true);


function onSomethingDown(evt:Event):void
{
    var pointDown:Point;

    if (evt.type == "mouseDown") {
        pointDown.x = MouseEvent(evt).stageX;
        pointDown.y = MouseEvent(evt).stageY;
    } else if (evt.type == "touchBegin") {
        pointDown.x = TouchEvent(evt).stageX;
        pointDown.y = TouchEvent(evt).stageY;
    } 
}
0
On

Just use MouseEvent instead of TouchEvent. There is no real way to simulate a touch event using the AIR simulator (though you could use the official Android and iOS simulators to do so) other than doubling event listeners, as suggested already, which you want to avoid (it is best practice to eliminate as many event listeners as possible, especially on mobile).

From my experience, TouchEvent offers little-to-no benefit over MouseEvent. As far as I have been able to tell, they may be a little more precise but I have no evidence to back that up. It also support proximity events from things like a stylus. For the large majority of apps, though, there is no benefit.

MouseEvent will work on all platforms, including mobile (touches on a mobile device also fire off MouseEvents). And because TouchEvent maps out pretty much identically to MouseEvent (i.e. TOUCH_TAP to CLICK, TOUCH_BEGIN to MOUSE_DOWN). I currently have 8 AIR apps in the Play Store and 10 in the iTunes Store and not a single one uses TouchEvent, though some do utilize multitouch gestures.