2010-11-18 48 views
1

我如何移動原始源圖像而不是imageProxy?

<mx:Script> 
     <![CDATA[ 
      //Import classes so you don't have to use full names. 
      import mx.managers.DragManager; 
      import mx.core.DragSource; 
      import mx.events.DragEvent; 
      import flash.events.MouseEvent; 

      // Embed icon image. 
      [Embed(source='assets/globe.jpg')] 
      public var globeImage:Class; 

      // The mouseMove event handler for the Image control 
      // initiates the drag-and-drop operation. 
      private function mouseOverHandler(event:MouseEvent):void 
      {     
       var dragInitiator:Image=Image(event.currentTarget); 
       var ds:DragSource = new DragSource(); 
       ds.addData(dragInitiator, "img");    

       // The drag manager uses the Image control 
       // as the drag proxy and sets the alpha to 1.0 (opaque), 
       // so it appears to be dragged across the Canvas. 
       var imageProxy:Image = new Image(); 
       imageProxy.source = globeImage; 
       imageProxy.height=15; 
       imageProxy.width=15;     
       DragManager.doDrag(dragInitiator, ds, event, 
        imageProxy, -15, -15, 1.00); 
      } 

      // The dragEnter event handler for the Canvas container 
      // enables dropping. 
      private function dragEnterHandler(event:DragEvent):void { 
       if (event.dragSource.hasFormat("img")) 
       { 
        DragManager.acceptDragDrop(Canvas(event.currentTarget)); 
       } 
      } 

      // The dragDrop event handler for the Canvas container 
      // sets the Image control's position by 
      // "dropping" it in its new location. 
      private function dragDropHandler(event:DragEvent):void { 
       Image(event.dragInitiator).x = 
        Canvas(event.currentTarget).mouseX; 
       Image(event.dragInitiator).y = 
        Canvas(event.currentTarget).mouseY; 
      } 
     ]]> 
    </mx:Script> 

    <!-- The Canvas is the drag target --> 
    <mx:Canvas id="v1" 
     width="500" height="500" 
     borderStyle="solid" 
     backgroundColor="#DDDDDD" 
     dragEnter="dragEnterHandler(event);" 
     dragDrop="dragDropHandler(event);"> 

     <!-- The image is the drag initiator. --> 
     <mx:Image id="myimg" 
      source="@Embed(source='assets/globe.jpg')" 
      mouseMove="mouseOverHandler(event);"/> 
    </mx:Canvas> 
</mx:Application> 

在這個例子中imageProxy(複製圖像)拖動和移動。但我想移動原始源圖像dragInitiator不imageProxy。我能怎麼做 ?我試過像

DragManager.doDrag(dragInitiator, ds, event, 
         dragInitiator, -15, -15, 1.00); it's moving but deleted ? 

有什麼想法嗎?

回答

0

在我看來,你只是試圖在畫布中移動圖像,對嗎?如果是這樣,在我看來,使用DragManager和相關機制是矯枉過正的。您可以簡單地向圖像添加處理程序,以便鼠標向下/向上移動,然後添加一個成員變量「isMouseButtonDown」,或者當鼠標按鈕關閉時將其設置爲true,並且在鼠標按鈕打開時將其設置爲false。當鼠標出現故障時,向圖像添加一個處理程序以便鼠標移動(不要忘記在鼠標上移時移除處理程序)。然後,只要獲取移動事件,然後更新圖像的x/y,並且isMouseButtonDown爲真正。專業提示:當添加鼠標移動的處理程序時,將addEventListener的第三個參數設置爲true(useCapture),這會使圖像移動少得多。希望有所幫助。

相關問題