flex:拖放 - 对象居中

时间:2011-06-05 09:42:48

标签: flex drag-and-drop

在使用Flex的拖放情况下,我试图让对象中心对齐到某种程度,不管高度和宽度如何调整,它总是将落点定位到左上角。

这是代码..

imageX = SkinnableContainer(event.currentTarget).mouseX;
imageY = SkinnableContainer(event.currentTarget).mouseY;

// Error checks if imageX/imageY dont satisfy certain conditions- move to a default position
// img.width and img.height are both defined and traced to be 10- idea to center image to drop point

Image(event.dragInitiator).x = imageX-(img.width)/2;
Image(event.dragInitiator).y = imageY-(img.height)/2

最后两行似乎没有任何效果。任何想法为什么 - 必须是直截了当的,我错过了......

2 个答案:

答案 0 :(得分:3)

您可以使用以下代码段:

private function on_drag_start(event:MouseEvent):void
{
    var drag_source:DragSource = new DragSource();
    var drag_initiator:UIComponent = event.currentTarget as UIComponent;

    var thumbnail:Image = new Image();
    // Thumbnail initialization code goes here

    var offset:Point = this.localToGlobal(new Point(0, 0));
    offset.x -= event.stageX;
    offset.y -= event.stageY;
    DragManager.doDrag(drag_initiator, drag_source, event, thumbnail, offset.x + thumbnail.width / 2, offset.y + thumbnail.height / 2, 1.0);
}

这是一个重要的细节。该片段使用舞台坐标系。 如果使用event.localX和event.localY,则在某些情况下此方法将失败。例如,单击并拖动影片剪辑。如果使用localX和localY而不是舞台坐标,则localX和localY将在影片剪辑的当前单击部分中定义坐标,而不是在整个影片剪辑中。

答案 1 :(得分:1)

使用DragManager的doDrag方法中的xOffsetyOffset属性。

查看here示例。