Actionscript 3 在Actionscript 3中拖放

Actionscript 3 在Actionscript 3中拖放,actionscript-3,flash-cs5,Actionscript 3,Flash Cs5,我有两个对象和一个目标。当鼠标键按下时,我将拖动一个对象,当鼠标键向上时,将其放在目标上。当o2位于目标位置并将o1拖动到目标位置时,o2会移动到其位置,而o1不会移动到目标位置 有人能帮我吗 var t1:int=0; //target is empty o1.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); o1.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

我有两个对象和一个目标。当鼠标键按下时,我将拖动一个对象,当鼠标键向上时,将其放在目标上。当o2位于目标位置并将o1拖动到目标位置时,o2会移动到其位置,而o1不会移动到目标位置

有人能帮我吗

 var t1:int=0; //target is empty

 o1.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
 o1.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

 function startDragging(evt:MouseEvent):void
 {
o1.startDrag();
if (o1.x == tar1.x && o1.y == tar1.y)
{
    t1 = 0;
}
}
function stopDragging(evt:MouseEvent ):void
  {
    if (tar1.hitTestObject(o1)  && t1 == 0)
    {
        o1.x = tar1.x;
        o1.y = tar1.y;
        o1.stopDrag();
        t1 = 1; //target is full
    }
        else
    {
        o1.x = 250;
        o1.y = 200;
        o1.stopDrag();
    }
        }

   o2.addEventListener(MouseEvent.MOUSE_DOWN, startDragging2);
   o2.addEventListener(MouseEvent.MOUSE_UP, stopDragging2);

   function startDragging2(evt:MouseEvent):void
    {
o2.startDrag();
if (o2.x == tar1.x && o2.y == tar1.y)
{
    t1 = 0;
}
}
function stopDragging2(evt:MouseEvent ):void
   {
    if (tar1.hitTestObject(o2)   && t1 == 0)
    {
        o2.x = tar1.x;
        o2.y = tar1.y;
        o2.stopDrag();
        t1 = 1;}
        else 
    {
        o2.x = 250;
        o2.y = 140;
        o2.stopDrag();
    }
        }           

诀窍是,你的
o2
对象首先接收
MouseeEvent。当你拖动
o1
时,翻转
事件,然后接收
MOUSE\u UP
事件以及
o1
。然后,两个事件侦听器分别触发。第一个侦听器
stopDragging2
激活,检查
t1
“全局”变量,发现它为0-OK,
o1
设置在
目标上,然后
stopDragging2
触发器,bam,
t1
非零,因此
else
分支触发器和
o2
被替换回起始位置。通常的解决方案是从对象中添加和删除侦听器,并通过事件的
target
属性寻址对象,如下所示:

o1.addEventListener(MouseEvent.MOUSE_DOWN,startDragging);
o2.addEventListener(MouseEvent.MOUSE_DOWN,startDragging);
// note that listeners are not separate functions! We will
// be using event.target to check which object is being dragged
function startDragging(e:MouseEvent):void {
    e.target.removeEventListener(MouseEvent.MOUSE_DOWN,startDragging);
    // surprise! This object will not be moved via MOUSE_DOWN,
    // because it's already being moved
    e.target.addEventListener(
    e.target.startDrag(); // drag
} // no alteration to t1, as we don't really care if we're dragging 
// our object off target - yet
function stopDraggging(e:MouseEvent):void {
    e.target.stopDrag(); // stop dragging anyway
    e.target.removeEventListener(MouseEvent.MOUSE_UP,stopDragging);
    // and stop listening for mouse-ups
    if (e.target.hitTestObject(tar1)&& (t1==0)) {
        e.target.x=tar1.x;
        e.target.y=tar1.y;  
        t1=1; // target is full
        return; // emergency exit. We don't need to do more
    }
    // should you add more targets, you can put hitTest code here

    // if we reached this point, we don't hit any targets
    // so we should put our objects back into their positions. 
    // The best way would be storing their base coordinates on 
    // objects themselves, so we don't have to run elsewhere for them
    e.target.x=e.target.startingX; // make sure these are filled
    e.target.y=e.target.startingY;
    e.target.addEventListener(MouseEvent.MOUSE_DOWN,startDragging);
    // and enable this object to be dragged again.
}

非常感谢。我在考虑你的指示,维斯珀!你能帮我解决这个问题吗?当监听器在目标中时,您的解决方案会将其从o1或o2中移除,但我希望用户能够再次将其拖回原位,即使对象在目标中。在我的真实项目中,我有9个对象和7个目标。有没有一种方法可以让其他对象侦听器在拖动对象时禁用临时的?如果您希望能够将对象拖离目标,只需在代码的开头添加一个侦听器,而不是在代码的结尾。关于您的另一个问题,您应该跟踪是否已经有一个对象正在被拖动,如果有,就不要继续侦听器。