ActionScript MouseEvent的CLICK与DOUBLE_CLICK

时间:2010-04-11 01:59:19

标签: actionscript-3 click mouseevent double-click

是否可以在同一个显示对象上同时使用CLICK和DOUBLE_CLICK?我试图在双击舞台的阶段添加一个新对象并在舞台上单击一次取消选择所选对象。

似乎DOUBLE_CLICK将同时执行自身以及DOUBLE CLICK路径中的第一个CLICK功能(鼠标按下,鼠标按下,单击,鼠标按下,鼠标按下,双击)。

在我编程的其他语言中,有一个内置的计时器将两者分开。这在AS3中不可用吗?


更新

这是一些代码。基本上我想要的是一个或另一个,而不是双击

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)

3 个答案:

答案 0 :(得分:8)

好吧,您可以使用setTimeoutclearTimeout

它看起来像这样:

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);

答案 1 :(得分:5)

您是否将.doubleClickEnabled设为true?

您还应该看看here

答案 2 :(得分:1)

很棒的回答Wallacoloo - 谢谢你。我刚刚实现了你的解决方案并提炼了几点,所以我想我会把它放在这里供将来参考(当然还有溢出社区的好处!)。首先,我无法测试setTimeout返回的uint上的undefined,所以我用== 0条件替换了未定义的条件。其次,我想立即提交单击的逻辑(只是为了提供更愉快的用户界面),所以我做了一些改组:

        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);
        }