A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: [Resolved] [Resolved] [Resolved] [Resolved] Help Desperately Needed

Threaded View

  1. #8
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,875
    this code will double to detect whether the button has been single clicked or double clicked...

    Now the first part is pretty simple, that is making it respond to a double click. All you need to do is find the difference in time between the first click and the second click and if its approximately < 400ms a double click has been made. e.g: create a button myButton then the following code will trace out when you have made a double click

    Code:
    myButton.onRelease = function() {
    	var currTime = getTimer();
    	var diff = currTime-this.lastTime;
    	if (diff<400) {
    		trace("doubleclicked");
    	}
    	this.lastTime = currTime;
    };
    Now the tricky part is to determine whether a user intended a single click rather than a double click. Why? Well because in the process of a double click a single click always occurs. So we kind of need to determine whether a single click or double click was intended by the user.

    So the solution to this is as follows: After a single click has been made a setInterval is started (which after 401 ms will trace out that a single click has occured).

    But here's the trick, if the button is pressed again within that time period before the trace has occured, we clear that interval as then we know the user is double clicking the button. Hope that makes sense

    Code:
    myButton.onRelease = function() {
    	currButt = this;
    	clearInterval(this.nTimer);
    	var currTime = getTimer();
    	var diff = currTime-this.lastTime;
    	this.nTimer = setInterval(timer, 401);
    	if (diff<400) {
    		clearInterval(this.nTimer);
    		trace("doubleclicked");
    	}
    	function timer() {
    		clearInterval(currButt.nTimer);
    		trace("single clicked");
    	}
    	this.lastTime = currTime;
    };
    Note:
    Note there will a half second delay(401 ms to be exact) to determine whether it is a single click as this is the only way of knowing that a double click was not intended..because thats the time (400 ms) we use to determine whether a double click has occured

    So in a nutshell:

    A user clicks the button.

    If he doesnt click it again within 401 ms a single click is registered.
    If he clicks it again within <400ms then a double click is registered.
    Last edited by silentweed; 08-07-2006 at 08:23 AM.
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center