2011-02-24 78 views
1

我在flex(web)应用程序中添加了自定义拖放功能,除了少数weeny问题外,它一切正常。被丢弃的图像与实际的拖动代理略有不同。我想将图像拖放到拖动代理的确切位置。我从VBox拖到面板。我似乎无法建立实际代理坐标和下降坐标之间的连接。 Thanx很多:)Flex拖放到拖动代理的精确坐标

回答

2

这是一个老问题,但我确定其他人正在寻找这些信息。经过大量调试后,我确定了一种方法来使这项工作(Flex 4 ... 3可能类似)。 问题在于用户可以在任何位置抓取原始对象以开始拖动。当你放下对象时,你需要指定它的位置。要完美地放下它,您需要知道用户抓住对象的位置。我无法找到任何对DragEvent中“对象到鼠标”x,y坐标的引用传递给Drop事件处理程序。但是,这些信息是在Mouse Down处理程序中传递的原始MouseEvent(localX,localY)中提供的。所有你需要做的就是坚持这些coords(我将它们存储在被拖动的对象中作为鼠标偏移变量)。当放置事件发生时,只需从DragEvents currentTarget mouseX和mouseY中分别减去它们即可。无论出于什么原因,我还需要从两个坐标中减去1,以便完美降落。 下面是一段代码,希望能让它更清晰。

// draggable object mouse down handler 
private function mouseDownHandler(e:MouseEvent):void { 
    // MenuObject is one of my custom objects 
    var tempMenuObject:MenuObject = e.currentTarget as MenuObject; 

    // The MenuObject contains its own custom drag proxy 
    var tempProxy:Graphic = tempMenuObject.dragProxy; 

    // HERE !!! --- persist the "mouse to object" coords 
    tempMenuObject.mouseOffsetX = e.localX; 
    tempMenuObject.mouseOffsetY = e.localY; 

    DragManager.doDrag(e.currentTarget as IUIComponent, null, e, tempProxy); 
} 

// droppable containers drop handler 
private function dragDropHandler(e:DragEvent):void { 

    // HERE !!! --- set the dragged objects x y coords (needed to subtract ONE from both coords to get it perfect) 
    e.dragInitiator.x = (e.currentTarget.mouseX - MenuObject(e.dragInitiator).mouseOffsetX) - 1; 
    e.dragInitiator.y = (e.currentTarget.mouseY - MenuObject(e.dragInitiator).mouseOffsetY) - 1; 

    // add the item to the container 
    e.currentTarget.addElement(e.dragInitiator); 
} 

希望这样可以节省别人一些时间...