如何在另一个mc移入as3时移动mc

时间:2013-07-17 11:18:41

标签: actionscript-3 flash actionscript

我有一个以拇指命名的Mc。我有其他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);
}

2 个答案:

答案 0 :(得分:1)

简单,只需添加一行代码即可将track.x值设置为stage_onMouseMove函数内的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
}

答案 1 :(得分: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;
    ...
}