ActionScript 3.0 drag and drop one MovieClip at a time

I am creating a Flash file in which multiple frames can be dragged and moved around a scene. I've set up functions to control when they are dragged, but when I click on one MovieClip, the other instantly joins 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;

var egg:Egg = new Egg();
egg.x = 330;
egg.y = 330;

//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;



source to share

1 answer

because you are adding eventListener to create not for movieclip

//Milk Event listeners:;
milk.mouseChildren = false;
milk.addEventListener(MouseEvent.MOUSE_DOWN, startMilkDrag);
milk.addEventListener(MouseEvent.MOUSE_UP, stopMilkDrag);
milk.addEventListener(Event.ENTER_FRAME, dragMilk);

//Egg Event listeners:
egg.mouseChildren = false;
egg.addEventListener(MouseEvent.MOUSE_DOWN, startEggDrag);
egg.addEventListener(MouseEvent.MOUSE_UP, stopEggDrag);
egg.addEventListener(Event.ENTER_FRAME, dragEgg);




All Articles