Archive for February, 2010

Simulate Double-Click with Actionscript

February 2nd, 2010

Using the Flash built in solution to detect double clicks with MouseEvent.DOUBLE_CLICK can turn out to be a pain, especially if you’re developing more than a simple website (no offence to those who do! ;) ). For my current project (Level Master 2000), I urgently needed such behaviour, and I didn’t want to turn on sprite.doubleClickEnabled = true for all selectable objects. So I simply implemented my own version, for which I can even adjust the click interval, such that a double click gets recognized.
It’s as simple as:

private const DOUBLE_CLICK_INTERVAL:int = 400;
private var clickIntervalTimer:int;

Somewhere, add your event listener to a normal mouse down event:

%something%.addEventListener(MouseEvent.MOUSE_DOWN, OnClick);

And then, in the OnClick, check if it was a double click:

if((getTimer() - clickIntervalTimer) < DOUBLE_CLICK_INTERVAL) 
{
    //A DOUBLE CLICK HAPPENED, DO SOMETHING!
}
 
//Don't forget to update your clickIntervalTimer afterwards:
clickIntervalTimer = getTimer();

Note: the getTimer function can be found in import flash.utils.getTimer;. Everything else should be straight forward!