Double Click

Along with my Multi Key class I was needing for a recent project, I have also been required to write a few applications that needed some minor double click detection. I took this opportunity to write a very simple class for detecting double clicks. It’s really easy to use and has served it’s purpose for me, if anyone wants to elaborate on, or use this class… please feel free. I have included an example of the class and the code in the fla, and also a zip file with a ready example. Take Care.

DoubleClick.as:

import lists.*;
class DoubleClick
{
    // the target we are checking if is double clicked
    private var target:MovieClip = null;
    // the amount of time before the second click doesn't count as a double click
    private var DELAY:Number = 300;
    // a flag specifying whether or not the first click has been made
    private var firstClicked:Boolean = false;
    // the id of the interval that gets set to destroy the second click
    private var intID:Number;
    // the list that stores all objects listening for events
    private var listeners : ListenerList;
    /* Constructor */
    public function DoubleClick(target:MovieClip)
    {
        this.target = target;
        // listen for events from the mouse
        Mouse.addListener(this);
        listeners = new ListenerList();
    }
    // is called everytime the users presses the mouse
    private function onMouseDown():Void
    {
        if (target == null || target.hitTest(_xmouse, _ymouse))
        {
            if (!this.firstClicked)
            {
                this.firstClicked = true;
                intID = setInterval(killInterval, this.DELAY);
            }
            else if (this.firstClicked)
            {
                invokeOnDoubleClicked();
                this.firstClicked = false;
                killInterval();
            }
        }
    }
    // destroys the interval, making sure the double click gets destroyed
    private function killInterval():Void
    {
        this.firstClicked = false;
        clearInterval(intID);
    }
    
    // adding and removing listeners
    public function addListener( o : Object ) : Boolean
    {
        return listeners.addListener(o);
    }
    public function removeListener( o : Object ) : Boolean
    {
        return listeners.removeListener(o);
    }
    // inokes the onDoubleClicked() event in all listeners
    private function invokeOnDoubleClicked() : Void
    {
        for (var i:Number = 0; i<listeners.count; i++)
        {
            listeners.getListener(i).onDoubleClicked();
        }
    }
}

FLA code:


// create a new instance of DoubleClick,  you can either pass in the movieclip you want to double click
// or pass in nothing if you just want to listen for double clicks anywhere
var dc = new DoubleClick(test_mc);

// create a new instance of a listener object
_obj = new Object();
// our double click object is going to call onDoubleClicked() on all listeners, so we will need to define that function
_obj.onDoubleClicked = onDC;

// we then add _obj as a listener of our DoubleClick object
dc.addListener(_obj);

// this is the definition of our onDoubleClicked() function
function onDC() : Void
{
    output.text = "You double clicked the movieclip";
}

-Michael

This is the easy way I can explain it.

I have this ball that I can click and drag to move the ball around the page.

I then want to double click the ball. On double click it goes to a movie that makes the ball bounce and play bounce sound.

Does that make sense?

Can you adjust your source to fit this?