开发者

ActionScript MouseEvent's CLICK vs. DOUBLE_CLICK

is it not possible to have both CLICK and DOUBLE_CLICK on the same display object? i'm trying to have both for the stage where double clicking the stage adds a new object and clicking once on the stage deselects a selected object.

it appears that DOUBLE_CLICK will execute both itself as well as the first CLICK functions in the path toward DOUBLE CLICK (mouse down, mouse up, click, mouse down, mouse up, double click).

in other languages i've programmed with there was a built-in timers that set the two apart. is开发者_StackOverflow社区 this not available in AS3?


UPDATE

here's some code. essentially what i would like is have one or the other, not both with double click

stage.doubleClickEnabled = true;
stage.addEventListener(MouseEvent.DOUBLE_CLICK, twoClicks, false, 0, true);
stage.addEventListener(MouseEvent.CLICK, oneClick, false, 0, true);

function oneClick(evt:MouseEvent):void
    {
    trace("One CLICK");
    }

function twoClicks(evt:MouseEvent):void
    {
    trace("Two CLICKS");
    }

//oneClick trace = "One CLICK"
//twoClicks trace = "One CLICK Two CLICKS" (instead of just Two CLICKS)


Well, you could use setTimeout and clearTimeout.

It'd look something like this:

const var DOUBLE_CLICK_SPEED:int = 10;
var mouseTimeout;

function handleClick(evt:MouseEvent):void {
    if (mouseTimeout != undefined) {
        twoClicks();
        clearTimeout(mouseTimeout);
        mouseTimeout = undefined;
    } else {
        function handleSingleClick():void {
            oneClick();
            mouseTimeout = undefined;
        }
        mouseTimeout = setTimeout(handleSingleClick, DOUBLE_CLICK_SPEED);
    }
}

function oneClick(evt:MouseEvent):void {
    trace("One CLICK");
}

function twoClicks(evt:MouseEvent):void {
    trace("Two CLICKS");
}
stage.addEventListener(MouseEvent.CLICK, handleClick, false, 0, true);


Did you set .doubleClickEnabled to true?

You should also take a look here.


Great answer Wallacoloo - thanks for that. I have just implemented your solution and refined a few points, so I'd thought I'd put it here for future reference (and of course the benefit of the overflow community!). Firstly, I couldn't test for undefined on the uint returned by setTimeout, so I replaced the undefined conditional with an == 0 conditional. Secondly, I wanted to commit the logic of a single click instantaneously (just makes for a more pleasant user interface), so I've done a bit of reshuffling:

        if (mouseTimeout != 0) {
            // clicked within the timeout, handle as double click

            // rollback single click logic
            rollbackSingleClickHandler(e);
            // commit double click logic
            dblClickHandler(e);

            clearTimeOut(mouseTimeout);
            mouseTimeout = 0;
        } else {
            // first click of a click sequence

            // commit single click logic
            singleClickHandler(e); 

            function clearTime():void {
                mouseTimeout = 0;
            }

            // register a timeout for a potential double click
            mouseTimeout = setTimeout(clearTime, DOUBLE_CLICK_SPEED);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜