Drag and Drop sample
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”>
<mx:Script>
<![CDATA[
import mx.containers.Canvas;
import mx.containers.HBox;
import mx.controls.Alert;
import mx.events.DragEvent;
import mx.containers.VBox;
import mx.managers.DragManager;
import mx.core.DragSource;
import mx.controls.Image;
private var filesource:String;
private var draghbox:HBox=new HBox();
public function doDrag(eve:MouseEvent):void
{
var img:Image=eve.currentTarget as Image;
var ds:DragSource=new DragSource();
ds.addData(img,'img');
var dragProxy:Image=new Image();
dragProxy.source=img.source.toString();
filesource=img.source.toString();
DragManager.doDrag(img,ds,eve,dragProxy,-15,-15,1.0);
}
public function dragaccept(eve:DragEvent):void
{
var dropTarget:VBox=eve.currentTarget as VBox;
DragManager.acceptDragDrop(dropTarget);
}
public function dragDrop(eve:DragEvent):void
{
var img:Image=eve.dragSource.dataForFormat('img') as Image;
img=new Image();
img.source=filesource;
img.height=50;
img.width=50;
if(draghbox.numChildren%2==0||dropVbox.numChildren==0)
{
draghbox=new HBox();
dropVbox.addChild(draghbox);
}
draghbox.addChild(img);
}
public function dragacceptHandler(eve:DragEvent):void
{
var droptarget:Canvas=eve.currentTarget as Canvas;
DragManager.acceptDragDrop(droptarget);
}
public function dragdropHandler(eve:DragEvent):void
{
var img:Image=eve.dragSource.dataForFormat('img')as Image;
img=new Image();
img.source=filesource;
img.height=50;
img.width=50;
img.setStyle('verticalGap','50');
img.setStyle('horizontalGap','50');
cannew.addChild(img);
}
]]>
</mx:Script>
<mx:Canvas x=”34″ y=”62″ width=”200″ height=”167″ backgroundColor=”#E9E5E8″>
<mx:Image x=”25″ y=”31″ source=”Rose.png” mouseDown=”doDrag(event)”/>
<mx:Image x=”96″ y=”36″ source=”Tulip.png” mouseDown=”doDrag(event)”/>
<mx:ImageĀ source=”Lavendar.png” mouseDown=”doDrag(event)” x=”38″ y=”113″/>
<mx:ImageĀ source=”Magnolia.png” mouseDown=”doDrag(event)” x=”115″ y=”111″/>
</mx:Canvas>
<mx:VBox id=”dropVbox” x=”57″ y=”282″ height=”288″ width=”272″ dragEnter=”dragaccept(event)” dragDrop=”dragDrop(event)”
backgroundColor=”#4D1313″>
</mx:VBox>
<mx:Canvas id=”cannew” x=”401″ y=”282″ width=”280″ height=”288″ backgroundColor=”#12292F” dragEnter=”dragacceptHandler(event)”
dragDrop=”dragdropHandler(event)”>
</mx:Canvas>
</mx:Application>
No comments yet.
