Double clicking in Flash

Posted Sunday, August 28, 2005 9:09 AM by Shunjie
Ok, I know this is some old stuff which most of you probably already knows but I am posting it cause I see funny codes to detect a double click using onEnterframes and setInterval, etc. All you need is the code below to detect a double click in Flash. To prevent stuff like "triple click", just modify the code to execute

var pressCount:Number = 0;
var lastTime:Number = 0;
function onPress()
    {
        var now:Number = getTimer();
         pressCount++
        if((now - lastTime) < 300 &&  pressCount==2)
        {
           //Double click occurs, implement the code
            pressCount = 0;
        }
        else if(pressCount == 2)
        {
            pressCount = 1;
        }
        lastTime = now;
           
    }

We have to keep track of pressCount to prevent 'triple-clicking'. If you don't need to prevent that, just use

function onPress()
    {
        var now:Number = getTimer();
      
        if((now - lastTime) < 300)
        {
           //Double click occurs, implement the code
          //Triple Click will detect as 2 double click!
        }
        lastTime = now;
           
    }

Do comment if anything is wrong! =)
Filed under: ,

Comments

# re: Double clicking in Flash

Wednesday, April 05, 2006 5:34 PM by jabbypanda

Thank you for sharing this piece of code.

I use "double click" event in my two projects,  and I did not think yet about triple clicks - I will update my code.


My current code is :

// check for doubleclick event
private function isDoubleClick():Boolean {
var nowClickTime:Number = getTimer();

var dClick:Boolean = ((nowClickTime- this.lastDoubleClickTime < 350));

this.lastDoubleClickTime = nowClickTime;

return dClick;
}

# re: Double clicking in Flash

Wednesday, April 05, 2006 5:35 PM by jabbypanda

Thank you for sharing this piece of code.

I use "double click" event in my two projects,  and I did not think yet about triple clicks - I will update my code.


My current code is :

// check for doubleclick event
private function isDoubleClick():Boolean {
var nowClickTime:Number = getTimer();

var dClick:Boolean = ((nowClickTime- this.lastDoubleClickTime < 350));

this.lastDoubleClickTime = nowClickTime;

return dClick;
}