0

我试图在水平面上移动这个安全带对象。我已经发布了我到目前为止所做的事情。它并没有真正奏效,因为似乎一旦我按下按钮,安全带只能向右侧移动并且MOUSE_UP不起作用。有人可以指导我如何去做吗?

 seatBelt.addEventListener (MouseEvent.MOUSE_DOWN, prsing);
    seatBelt.addEventListener (MouseEvent.MOUSE_UP, lfting);
    seatBelt.addEventListener (MouseEvent.MOUSE_MOVE, mving);




    function prsing (e:MouseEvent){
    posX= seatBelt.x ;
        posY = seatBelt.y ;
        mouseclick = 1;


    }
    function mving (e:MouseEvent) {
        if (mouseclick == 1) {
        seatBelt.x = mouseX ;
        }
    }

    function lfting (e:MouseEvent){
        seatBelt.x = posX;
        seatBelt.y = posY;
        mouseclick =0;

    }
4

3 回答 3

1

因此,您的预期功能是能够沿 x 轴拖动安全带并在释放时让它回到原来的位置?

您需要更改 MOUSE_UP 和 MOUSE_MOVE 来听舞台而不是安全带。这是因为当按钮不再位于安全带上方时,您可以释放按钮,因此该函数永远不会被调用。然而,舞台将接收该事件。

stage.addEventListener(MouseEvent.MOUSE_UP, lifting);
stage.addEventListener(MouseEvent.MOUSE_MOVE, moving);

我不确定您在哪里声明mouseX变量,但如果您在拖动功能后将侦听器更改为:

   function moving (e:MouseEvent) {
        if (mouseclick == 1) {
        seatBelt.x = e.stageX;
        }
    } 
于 2013-03-27T01:44:39.507 回答
0

你所拥有的大部分看起来还不错,除了我认为正在发生的事情是你在舞台上/在安全带对象之外的某个地方“鼠标移动”。我猜这是为了让物体移动你让它对鼠标移动做出反应......我猜你正在将鼠标移动到整个舞台并离开安全带物体,此时你释放鼠标。

要检查是否是这种情况,请尝试在您的安全带对象上释放鼠标,以查看您的 MOUSE_UP 事件是否被触发。

我猜你想要的行为是你想让对象在任何时候停止移动,无论鼠标在哪里释放。为此,请尝试将 MOUSE_UP 事件侦听器添加到舞台:

this.stage.addEventListener (MouseEvent.MOUSE_UP, lfting);

但是,由于您可能并不总是希望此侦听器处于活动状态,因此可能仅在鼠标在对象上按下时添加它,方法是仅根据需要添加和删除侦听器。

我已经对您的代码进行了一些编辑以显示我的意思并取出“mouseclick”布尔值,因为它不再需要跟踪鼠标按下/向上事件:

seatBelt.addEventListener(MouseEvent.MOUSE_DOWN, prsing);

function prsing(e:MouseEvent){
    posX= seatBelt.x ;
    posY = seatBelt.y ;
    this.stage.addEventListener(MouseEvent.MOUSE_MOVE, mving);
    this.stage.addEventListener(MouseEvent.MOUSE_UP, lfting);
}

function mving(e:MouseEvent) {
    seatBelt.x = mouseX;
}

function lfting(e:MouseEvent){
    seatBelt.x = posX;
    seatBelt.y = posY;
    this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mving);
    this.stage.removeEventListener(MouseEvent.MOUSE_UP, lfting);
}
于 2013-03-27T01:49:30.780 回答
0

您可以使用 Sprite.startDrag 方法来做您需要的事情。不需要 MOUSE_MOVE 侦听器。如果需要,请查找参数:Sprite。 我喜欢一次只有一个 MOUSE_DOWN 或 MOUSE_UP 侦听器处于活动状态。

seatBelt.addEventListener (MouseEvent.MOUSE_DOWN, prsing);

function prsing (e:MouseEvent){
    seatBelt.startDrag(false, new Rectangle(0,seatBelt.y,stage.width,seatBelt.y));
    seatBelt.removeEventListener (MouseEvent.MOUSE_DOWN, prsing);
    seatBelt.addEventListener (MouseEvent.MOUSE_UP, lfting);
}

function lfting (e:MouseEvent){
    seatBelt.stopDrag();
    seatBelt.addEventListener (MouseEvent.MOUSE_DOWN, prsing);
    seatBelt.removeEventListener (MouseEvent.MOUSE_UP, lfting);
}
于 2013-03-27T01:51:08.773 回答