Venki
Venki

Reputation: 7

How to Move a mc while the other mc moves in as3

I Have a Mc named as thumb. And i have other mc named as track. When i move the thumb_mc using my script below, i also need my track_mc to move.

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

Upvotes: 0

Views: 90

Answers (2)

Mahmoud Badri
Mahmoud Badri

Reputation: 1296

Simple, just add one line of code to set the track.x value to the thumb.x inside the stage_onMouseMove function.

One important thing to note is to add it at the end of the function so that it received the value after it is updated with the bounding check, like this:

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
}

Upvotes: 1

jfgi
jfgi

Reputation: 107

You can modify your mouse_move a bit:

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

Or if you need to move track ONLY when thumb is moving you can do following:

Add variable to store previous thumb position var previousPos:int;

In mouse_down add such code previousPos = thumb.x;

And then modify mouse move in such way:

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

Upvotes: 0

Related Questions