2011-06-05 77 views
1

在使用Flex的拖放情況下,我試圖讓對象中心對齊到放置點 - 無論如何調整高度和寬度,它總是定位將點滴落到左上方。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行似乎沒有任何效果。任何想法,爲什麼,一定有什麼簡單的,我很想念......

回答

1

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

查看here爲例。

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將定義影片剪輯的當前單擊部分中的座標,而不是整個影片剪輯中的座標。

+0

非常感謝。保存了我的一天:) – 2012-07-09 15:08:33