0

这基本上只是拖动并双击进行设置(因此暂时禁用了拖动),但精灵不与鼠标保持一致 - 有人可以指出我更好的代码 - 否则我会继续这样做 - 还有很多事情要做.

//The initial event performed when the button is first clicked;
internal var m_nDoubleClickSpeed:Number = 300;
internal var m_toMouse:Number;
internal var moveready:Boolean = false;
internal var initalx:uint;
internal var initialy:uint;
internal var move:Boolean;

internal function clickDoubleClick(e:MouseEvent):void {
    if (isSet == false) {
        this.startDrag();
    }

    if (isNaN(m_toMouse)==false) {
        clearTimeout(m_toMouse);
        HandleDoubleClick();
    } else {
        m_toMouse = setTimeout(HandleSingleClick, m_nDoubleClickSpeed);
    }
}

internal function HandleSingleClick():void {
    trace("HandleSingleClick");
    trace("isSet");
    trace(isSet);
    this.stopDrag();

     m_toMouse = NaN;
}

internal function HandleDoubleClick():void {
    if (isSet == false) {
        isSet = true;
        this.stopDrag()
    } else {
        isSet = false; 
    }

    trace("HandleDoubleClick");
    m_toMouse = NaN;  
}

internal function goodX(inX:Number):Number {
    if (inX < 0) {
        return 0;
    }

    if (inX > (stage.stageWidth) ) {       
        return (stage.stageWidth);
    }

    return inX;
}

internal function goodY(inY:Number):Number {
    if (inY < 0) {
        return 0;
    }

    if (inY > stage.stageHeight) {
        return stage.stageHeight;
    }
    return inY;
} 
4

1 回答 1

0

我不确定我是否理解正确。所以你想开始拖动单击并拖动直到双击对吗?如果是这样,你可以尝试这样的事情:

public class ClickAndDrag extends Sprite
{
    private var clickTimeout:uint;
    private var doubleClickSpeed:int = 500;
    private var clickedOnce:Boolean;
    private var mouseOnClick:Point;
    private var isDragging:Boolean;

    public function ClickAndDrag() 
    {
        graphics.beginFill(Math.random()*0xffffff, 1);
        graphics.drawCircle(0, 0, 20);
        graphics.endFill();

        this.buttonMode = true;
        addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
    }

    private function handleMouseDown(e:MouseEvent):void 
    {
        if (isDragging)
        {
            if (clickedOnce)
                handleDoubleClicked();
            else
            {
                //if it's not clicket within next doubleClickSpeed ms then doubleClickSpeed will be set to false;
                clickedOnce = true;
                clickTimeout = setTimeout( handleClickTimeout, doubleClickSpeed );
            }
        }
        else
        {
            handleClickAndDrag();
        }
    }

    //clicked once when dragging
    private function handleClickOnce():void 
    {
        graphics.clear();
        graphics.beginFill(Math.random()*0xffffff, 1);
        graphics.drawCircle(0, 0, 20);
        graphics.endFill();
    }

    //clicked once when not dragging
    private function handleClickAndDrag():void 
    {
        isDragging = true;
        this.addEventListener(Event.ENTER_FRAME, handleFrame);
        mouseOnClick = new Point(this.mouseX, this.mouseY);
    }

    //doubleclicked when dragging
    private function handleDoubleClicked():void 
    {
        clearTimeout(clickTimeout);
        clickedOnce = false;            
        this.removeEventListener(Event.ENTER_FRAME, handleFrame);
        isDragging = false;
    }

    private function handleClickTimeout():void 
    {
        clickedOnce = false;
        handleClickOnce();
    }

    private function handleFrame(e:Event):void 
    {
        this.x = stage.mouseX - mouseOnClick.x;
        this.y = stage.mouseY - mouseOnClick.y;
    }


}

它基本上等待鼠标按下,如果它已经在拖动它,它会检查您是否单击了一次(更改颜色)或两次(停止拖动)。或者,如果它还没有拖动,那么它开始拖动。您可能还想处理离开舞台(Event.MOUSE_LEAVE)。

于 2013-10-16T16:32:15.293 回答