Drag and rotate several movieclips at once
Hi there,
I’ve been trying everything I can think of and looking everywhere for a solution but I can’t get my head around this basic problem.
I have several “Masters” and several “Slaves” movieclips. Both Masters and Slaves can be dragged and rotated (keyboard event) individually inside a MouseDown event, and then dropped inside a MouseUp event.
What I’d like to do is when one or several Slaves clips are touching one Master clip, dragging and rotating the Master will also drag and rotate the Slaves; the Master basically acting as a container.
I got that part working but there is something wrong with the MouseUp event once the Master clip has been rotated and dropped: the Slaves keep changing position with each rotation; while clicking back (MouseDown) on the Master clip put all the Slaves back to their right position.
Here is a sample code with one master and one slave:
----------------------------
Slave.addEventListener(MouseEvent.MOUSE_DOWN, fl_Drag_Slave);
Master.addEventListener(MouseEvent.MOUSE_DOWN, fl_Drag_Master);
// Drag Slave
function fl_Drag_Slave(event:MouseEvent):void
{
Slave.startDrag(); // Drag
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_Slave_Rotation); // Rotation event listener
stage.addEventListener(MouseEvent.MOUSE_UP, fl_Drop_Slave); // MouseUp event listener
}
// Drop Slave
function fl_Drop_Slave(event:MouseEvent):void
{
Slave.stopDrag();
stage.removeEventListener(KeyboardEvent.KEY_DOWN, fl_Slave_Rotation);
stage.removeEventListener(MouseEvent.MOUSE_UP, fl_Drop_Slave);
}
// Rotate Slave
function fl_Slave_Rotation(event:KeyboardEvent):void
{
if (event.keyCode == 82) // Keycode for the letter "r"
{
Slave.rotation += 45;
}
}
// Drag Master (and Slaves)
function fl_Drag_Master(event:MouseEvent):void
{
if (Slave.hitTestObject(Master))
{
Master.addChild(Slave); // Add the Slave to the Master
Slave.x -= Master.x; // Adjust the Slave's position and rotation
Slave.y -= Master.y;
Slave.rotation -= Master.rotation;
}
Master.startDrag();
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_Master_Rotation); // Rotation event listener
stage.addEventListener(MouseEvent.MOUSE_UP, fl_Drop_Master); // MouseUp event listener
}
// Drop Master (and Slaves)
function fl_Drop_Master(event:MouseEvent):void
{
if (Slave.hitTestObject(Master))
{
addChild(Slave); // Add the Slave back to the stage
Slave.x += Master.x; // Adjust the Slave's position and rotation
Slave.y += Master.y;
Slave.rotation += Master.rotation;
}
Master.stopDrag();
stage.removeEventListener(KeyboardEvent.KEY_DOWN, fl_Master_Rotation);
stage.removeEventListener(MouseEvent.MOUSE_UP, fl_Drop_Master);
}
// Rotate Master (and Slaves)
function fl_Master_Rotation(event:KeyboardEvent):void
{
if (event.keyCode == 82) // keycode for the letter "r"
{
Master.rotation += 45;
}
}
----------------------------
I think the problem resides in the way I adjust the Slaves clips’ positions when they are added back to the stage during the MouseUp event but I can’t get it to work.
Thanks a lot for your help!
