Skip to main content
Participant
May 5, 2015
Answered

How to drag and drop multiple movieclips at the same time

  • May 5, 2015
  • 3 replies
  • 628 views

Hi everyone,

I’m a new actonscript 3 and adobe flash CC user and I’m building an application but I've been stuck for several days now. I've looked everywhere and tried everything I could think of but I still can’t find a solution to my problem which I thought was pretty basic.

Basically, let’s say I have one rectangle and one circle on the stage. Once I’ve turned them into movieclips and assigned an instance name to each one of them, I’d like to be able to perform a drag and drop on the rectangle so that both the rectangle and the circle become draggable at the same time.

For now, I have a mouse-down event listener attached to the rectangle instance, a startDrag method assigned to the rectangle instance and another one assigned to the circle. But in this configuration, when I click and drag the rectangle, only the circle is moveable (only the last line in the code is taken into account).

I don’t know if what I’m trying to achieve is feasible but any help will be greatly appreciated, thanks!

This topic has been closed for replies.
Correct answer Ned Murphy

The startDrag() method can only work for one object at a time, so in your case the last one designated the task processes it.  One way to approach this is to temporarily plant the two objects in a container and drag the container.

rectangle.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);  // when I click the rectangle

var dragMC:MovieClip = new MovieClip();
addChild(dragMC);

function fl_ClickToDrag(event:MouseEvent):void

{    
  dragMC.addChild(rectangle);        // move the objects into the container
  dragMC.addChild(circle);
  dragMC.startDrag();
}


stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop); // when I release the mouse


function fl_ReleaseToDrop(event:MouseEvent):void

{    
  dragMC.stopDrag();
  rectangle.x += dragMC.x;         // adjust the positions of the objects for their new location
  rectangle.y += dragMC.y;
  circle.x += dragMC.x;
  circle.y += dragMC.y
  addChild(rectangle);                 // move the objects back to the stage
  addChild(circle);
  dragMC.x = dragMC.y = 0;       // reset the reference for the dragMC
}

All of that repositioning stuff in the Drop function is needed because when you drag the container, the positions of the contents are still at their original coordinates inside the container.  So when you drop them they will resume their x/y positions in their new parent, meaning they move back where they were.  To reposition them where they were dragged you need to account for the change in position of the dragMC.

3 replies

Participant
May 5, 2015

Argh, it wasn't supposed to show up like that and I can't get it better than that sorry:

rectangle.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);  // when I click the rectangle

function fl_ClickToDrag(event:MouseEvent):void

{    

  rectangle.startDrag(); // I want both the rectangle and    

  circle.startDrag(); // the circle to be draggable

}

stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop); // when I release the mouse

function fl_ReleaseToDrop(event:MouseEvent):void

{    

  rectangle.stopDrag(); // I want both the rectangle and    

  circle.stopDrag(); // the circle to stop

}

Ned Murphy
Ned MurphyCorrect answer
Legend
May 5, 2015

The startDrag() method can only work for one object at a time, so in your case the last one designated the task processes it.  One way to approach this is to temporarily plant the two objects in a container and drag the container.

rectangle.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);  // when I click the rectangle

var dragMC:MovieClip = new MovieClip();
addChild(dragMC);

function fl_ClickToDrag(event:MouseEvent):void

{    
  dragMC.addChild(rectangle);        // move the objects into the container
  dragMC.addChild(circle);
  dragMC.startDrag();
}


stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop); // when I release the mouse


function fl_ReleaseToDrop(event:MouseEvent):void

{    
  dragMC.stopDrag();
  rectangle.x += dragMC.x;         // adjust the positions of the objects for their new location
  rectangle.y += dragMC.y;
  circle.x += dragMC.x;
  circle.y += dragMC.y
  addChild(rectangle);                 // move the objects back to the stage
  addChild(circle);
  dragMC.x = dragMC.y = 0;       // reset the reference for the dragMC
}

All of that repositioning stuff in the Drop function is needed because when you drag the container, the positions of the contents are still at their original coordinates inside the container.  So when you drop them they will resume their x/y positions in their new parent, meaning they move back where they were.  To reposition them where they were dragged you need to account for the change in position of the dragMC.

Participant
May 5, 2015

Hooray it works!

Thanks a lot for your help, your time, and for the explanation, I will look closer to the code but for now it does what I was after!

Participant
May 5, 2015

Hi,

Alright, so my code is extremely basic since it's only a test to achieve what I want:

rectangle.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);  // when I click the rectangle

function fl_ClickToDrag(event:MouseEvent):void

{

    rectangle.startDrag(); // I want both the rectangle and

    circle.startDrag(); // the circle to be draggable

}

stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop); // when I release the mouse

function fl_ReleaseToDrop(event:MouseEvent):void

{

    rectangle.stopDrag(); // I want both the rectangle and

    circle.stopDrag(); // the circle to stop

}

In this configuration, when I click on the rectangle only the circle is draggable ... Only the last startDrag method is taken into account.

Thank you for your help.

Ned Murphy
Legend
May 5, 2015

Can you convert that code from paragraph form to something more easily readable?

Ned Murphy
Legend
May 5, 2015

You should show your code if you want help with it.