Image drag and drop

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Script>
		<![CDATA[
			import mx.controls.Image;
			import mx.events.DragEvent;
			import mx.core.DragSource;
			import mx.managers.DragManager;
			import flash.events.DataEvent;

			[Embed (source='7.gif')]

			private var globImage:Class;

			private function mouseOverHandler(event:MouseEvent):void
			{
				var dragInitiator:Image=Image(event.currentTarget);
				var ds:DragSource= new DragSource();
				ds.addData(dragInitiator,'img');

				var imageProxy:Image = new Image();
				imageProxy.source=globImage;
				imageProxy.height=10;
				imageProxy.width=10;
				DragManager.doDrag(dragInitiator, ds , event , imageProxy , -10 , -10 , 1.00);
			}
			private function dragEnterHandler(event:DragEvent):void
			{
				if(event.dragSource.hasFormat('img'))
				{
					DragManager.acceptDragDrop(Canvas(event.currentTarget));
				}
			}
			private function dragDropHandler(event:DragEvent):void
			{
				Image(event.dragInitiator).x =
				Canvas(event.currentTarget).mouseX;
				Image(event.dragInitiator).y =
				Canvas(event.currentTarget).mouseY;
			}

		]]>
	</mx:Script>

	    <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='7.gif')"
            mouseMove="mouseOverHandler(event);"/>
            <mx:TextArea text="Location in Canvas: x = {String(myimg.x)}, and y = {String(myimg.y)}" height="37" width="394" x="36" y="480"/>
    </mx:Canvas>

</mx:Application>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s