我一直在创建一个Flash文件,可以在舞台上拖动和移动多个MovieClip。我已经设置了控制何时被拖动的功能,但是当我点击一个MovieClip时,另一个立即将自己附加到另一个。我该如何解决这个问题?
以下是代码:
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;
}
}
答案 0 :(得分:2)
因为你将stageListener添加到舞台而不是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);