Skip to content

Instantly share code, notes, and snippets.

@rozd
Created June 12, 2016 08:56
Show Gist options
  • Save rozd/d77139f2a045305a043a0494b416b638 to your computer and use it in GitHub Desktop.
Save rozd/d77139f2a045305a043a0494b416b638 to your computer and use it in GitHub Desktop.
Fixes issue with significantly short delay between TOUCH_BEGIN and TOUCH_END events at the bottom of the Adobe AIR NOT full screen application.
/**
* Created by max on 6/12/16.
*/
package
{
import flash.display.Stage;
import flash.events.TimerEvent;
import flash.events.TouchEvent;
import flash.utils.Timer;
import flash.utils.getTimer;
/**
* Fixes issue with significantly short delay between TOUCH_BEGIN and TOUCH_END
* events at the bottom of the Adobe AIR NOT full screen application.
*
* The issue which this class fixes could lead to unresponsive button when
* "down" state is missing but button still works.
*/
public class FixUnresponsiveTouch
{
//--------------------------------------------------------------------------
//
// Constructor
//
//--------------------------------------------------------------------------
public function FixUnresponsiveTouch(stage:Stage, minDelay:Number = 40)
{
super();
this.stage = stage;
this.stage.addEventListener(TouchEvent.TOUCH_BEGIN, touchBeginHandler);
this.stage.addEventListener(TouchEvent.TOUCH_END, touchEndHandler, true, int.MAX_VALUE);
this.minDelay = minDelay || 40;
}
//--------------------------------------------------------------------------
//
// Variables
//
//--------------------------------------------------------------------------
private var stage:Stage;
private var minDelay:Number;
private var touches:Object = {};
//--------------------------------------------------------------------------
//
// Event handlers
//
//--------------------------------------------------------------------------
private function touchBeginHandler(event:TouchEvent):void
{
touches[event.touchPointID] = event.timestamp;
}
private function touchEndHandler(event:TouchEvent):void
{
var time:Number = event.timestamp - touches[event.touchPointID];
if (time < minDelay)
{
event.stopPropagation();
var timer:Timer = new Timer(minDelay - time, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent):void
{
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, arguments.callee);
event.timestamp = getTimer();
stage.dispatchEvent(event);
});
timer.start();
}
else
{
delete touches[event.touchPointID];
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment