I have been creating a Flash file where more than one MovieClip can be dragged and moved around the stage. I have set up functions up to control when it is being dragged but when I click on one MovieClip the other instantly attaches itself to the other. How do I get around this?
Here is the code:
import flash.geom.Point;
//Add variables and items, such as milk and eggs to stage:
var milkClickOffset:Point = null;
var eggClickOffset:Point = null;
var milk:Milk = new Milk();
milk.x = 250;
milk.y = 250;
addChild(milk);
var egg:Egg = new Egg();
egg.x = 330;
egg.y = 330;
addChild(egg);
//Milk Event listeners:;
addEventListener(MouseEvent.MOUSE_DOWN, startMilkDrag);
addEventListener(MouseEvent.MOUSE_UP, stopMilkDrag);
addEventListener(Event.ENTER_FRAME, dragMilk);
//Egg Event listeners:
addEventListener(MouseEvent.MOUSE_DOWN, startEggDrag);
addEventListener(MouseEvent.MOUSE_UP, stopEggDrag);
addEventListener(Event.ENTER_FRAME, dragEgg);
//Milk Functions:;
function startMilkDrag(event:MouseEvent):void
{
milkClickOffset = new Point(event.localX,event.localY);
}
function stopMilkDrag(event:MouseEvent):void
{
milkClickOffset = null;
}
function dragMilk(event:Event):void
{
if (milkClickOffset != null)
{// must be dragging
milk.x = mouseX - milkClickOffset.x;
milk.y = mouseY - milkClickOffset.y;
}
}
//Egg Functions:
function startEggDrag(event:MouseEvent):void
{
eggClickOffset = new Point(event.localX,event.localY);
}
function stopEggDrag(event:MouseEvent):void
{
eggClickOffset = null;
}
function dragEgg(event:Event):void
{
if (eggClickOffset != null)
{// must be dragging
egg.x = mouseX - eggClickOffset.x;
egg.y = mouseY - eggClickOffset.y;
}
}