2014-03-19 43 views
0

我有两个MovieClip(dragable和targetm)。我想要我的ActionScript,以便可以拖动targetm,并且如果将targetm放置在可拖动的位置,则它仍然处于可拖动状态,否则它会重置为其原始位置。这是我的ActionScript:ActionScript 3 - 目标不识别它被丢弃的位置

dragable.addEventListener(MouseEvent.MOUSE_DOWN, pickup); 
dragable.addEventListener(MouseEvent.MOUSE_UP, place); 

var startingLocation = new Point(); 

function pickup(evt:MouseEvent) { 

    startingLocation.x = evt.target.x; 
    startingLocation.y = evt.target.y; 

    evt.target.startDrag(); 
} 

function place(evt:MouseEvent) { 
    if (evt.target.dropTarget == targetm) { 

     trace('here'); 
     evt.target.stopDrag(); 

    } else { 

     evt.target.stopDrag(); 
     evt.target.x = startingLocation.x; 
     evt.target.y = startingLocation.y; 
    } 
} 

但即使我拖放在targetm顶部的dragable,它不会追踪任何东西。它转到else语句并重置dragable的位置。即使我将dragabale放在targetm顶端,它如何不能追踪任何东西?

注:如果我做

trace(evt.target.dropTarget); 

,如果我在targetm下降dragable的痕迹,它

[object Shape] 

回答

0

显示列表上还有其他东西正在返回evt.target.dropTarget;@Craig也是正确的,鼠标光标必须驻留在显示对象上。既然这可能会也可能不是这样,并且在将来你可能会有也可能没有什么东西,但我建议根据位置来做这件事。对于这一点,我将使用的getBounds建议,让两个坐标您dropTargettargetm的系统,驻留在同一个坐标空间:

function place(evt:MouseEvent):void { 
    var targetBounds:Rectangle = evt.target.getBounds(stage); 
    var targetmBounds:Rectangle = targetm.getBounds(stage); 

    if (targetBounds.intersects(targetmBounds)) { 
     trace('here'); 
     evt.target.stopDrag(); 
    } else { 
     evt.target.stopDrag(); 
     evt.target.x = startingLocation.x; 
     evt.target.y = startingLocation.y; 
    } 
} 
1

当你放下的是targetm内的整个dragable?如果没有,那么你的鼠标光标将不得不超过目标以获得跟踪。