0

我有一个叫拇指的 Mc。我还有其他名为 track 的 mc。当我使用下面的脚本移动 thumb_mc 时,我还需要我的 track_mc 移动。

thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumb_onMouseDown);
function thumb_onMouseDown(event:MouseEvent):void {
xOffset = mouseX - thumb.x;
stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
}

function stage_onMouseMove(event:MouseEvent):void {
thumb.x = mouseX - xOffset;
//restrict the movement of the thumb:
if(thumb.x < 8) {
    thumb.x = 8;
}
if(thumb.x > 540) {
    thumb.x = 540;
}

event.updateAfterEvent();
}
function stage_onMouseUp(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, stage_onMouseMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
}
4

2 回答 2

1

很简单,只需添加一行代码,在 stage_onMouseMove 函数中将 track.x 的值设置为 thumb.x。

需要注意的一件重要事情是将它添加到函数的末尾,以便它在使用边界检查更新后接收到值,如下所示:

function stage_onMouseMove(event:MouseEvent):void {
thumb.x = mouseX - xOffset;
//restrict the movement of the thumb:
    if(thumb.x < 8) {
        thumb.x = 8;
    }
    if(thumb.x > 540) {
        thumb.x = 540;
    }

    track.x = thumb.x; // move track with the thumb
}
于 2013-07-17T13:45:14.000 回答
0

您可以稍微修改一下 mouse_move:

function stage_onMouseMove(event:MouseEvent):void {
   thumb.x = mouseX - xOffset;
   // move your track also
   track.x = mouseX - someXOffset;
   track.y = mouseY - someYOffset;
   ...
}

或者,如果您只需要在拇指移动时移动轨道,您可以执行以下操作:

添加变量以存储先前的拇指位置var previousPos:int

在 mouse_down 添加这样的代码previousPos = thumb.x

然后以这种方式修改鼠标移动:

function stage_onMouseMove(event:MouseEvent):void {
    thumb.x = mouseX - xOffset;
    //restrict the movement of the thumb:
    if(thumb.x < 8) {
       thumb.x = 8;
    }
    if(thumb.x > 540) {
       thumb.x = 540;
    }
    if(previousPos != thumb.x){
        //moving track here
        track.x = somevalue;
    }
    previousPos = track.x;
    ...
}
于 2013-07-17T11:33:19.683 回答