| Home | Tutorials | Forums | Articles | Blogs | Movies | Library | Employment | Press | Buy templates |
|
|
#1 |
|
Registered User
Join Date: Feb 2006
Posts: 22
|
Hi,
I am using sample code from flex 2. The sample work for one image. But If I have multiple image, I don't know how to get the reference to the selected image private function doDragDrop(event { //var data:Object = event.dragSource.dataForFormat('img'); //HOW TO GET THE CURRENT SELECTED IMAGE HERE // myimg.x = target1.mouseX - xOff // myimg.y = target1.mouseY - yOff } Code:
<?xml version="1.0" ?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<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;
import mx.controls.Image;
import mx.containers.Canvas;
//Variables used to hold the image's location
public var xOff:Number;
public var yOff:Number;
// Embed icon image.
[Embed(source='globe.jpg')]
public var globeImage:Class;
// Drag initiator event handler, called by // the image's mouseMove event.
private function dragMe(event:MouseEvent, img1:Image, format:String):void
{
var dragInitiator:Image=Image(event.currentTarget);
var ds:DragSource = new DragSource();
ds.addData(img1, format);
// The drag manager uses the image as the drag proxy
// and sets the alpha to 100 (opaque),
// so it appears to be dragged across the canvas.
var imageProxy:Image = new Image();
imageProxy.source = globeImage;
imageProxy.height=10;
imageProxy.width=10;
DragManager.doDrag(dragInitiator, ds, event, imageProxy, 0, 0, 1.00);
}
//Function called by the canvas dragEnter event; enables dropping
private function doDragEnter(event:DragEvent):void
{
DragManager.acceptDragDrop(Canvas(event.target));
}
// Function called by the canvas dragDrop event;
// Sets the image object's position,
// "dropping" it in its new location.
private function doDragDrop(event:DragEvent, target1:Canvas, format:String):void
{
//var data:Object = event.dragSource.dataForFormat('img');
//HOW TO GET THE CURRENT SELECTED IMAGE HERE
// myimg.x = target1.mouseX - xOff
// myimg.y = target1.mouseY - yOff
}
// Helper function called by the dragged image's mouseMove event,
// as the image drags across the canvas.
// The function updates the xOff and yOff variables to show the
// current mouse location.
private function myoffset(img:Image):void {
xOff = img.mouseX
yOff = img.mouseY
}
]]>
</mx:Script>
<!-- The Canvas is both the drag target -->
<mx:Canvas id="v1" width="500" height="500" dragEnter="doDragEnter(event)"
dragDrop ="doDragDrop(event, v1, 'img')" borderStyle="solid"
backgroundColor="#DDDDDD">
<!-- The image is the drag initiator and the drag proxy. -->
<mx:Image id="myimg1" source="@Embed(source='globe.jpg')"
mouseMove="dragMe(event, myimg1, 'img');myoffset(myimg1);"/>
<mx:Image id="myimg2" source="@Embed(source='globe.jpg')"
mouseMove="dragMe(event, myimg2, 'img');myoffset(myimg2);"/>
<mx:Image id="myimg3" source="@Embed(source='globe.jpg')"
mouseMove="dragMe(event, myimg3, 'img');myoffset(myimg3);"/>
</mx:Canvas>
</mx:Application>
|
|
|
|
|
|
#2 |
|
Registered User
Join Date: Feb 2006
Posts: 22
|
I found out the solution after spending one hour reading.
private function doDragDrop(eventragEvent, target1:Canvas, format:String):void { var data:Object = event.dragSource.dataForFormat('img'); data.x = target1.mouseX - xOff data.y = target1.mouseY - yOff } |
|
|
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Drag & drop | picklejar | ActionScript 2.0 | 0 | 02-27-2006 02:27 PM |
| buttons in drag & drop map | nzassenhaus | Flash 8 General Questions | 1 | 12-09-2005 04:52 PM |
| Drag and Drop 3 items on 3 hit states... then you can continue | yinyang042 | ActionScript 1.0 (and below) | 3 | 10-26-2005 09:10 PM |
| random drag and drop | nicky | ActionScript 1.0 (and below) | 10 | 08-02-2004 10:23 AM |
| drag and drop to any drop target | rossbruniges | ActionScript 2.0 | 4 | 06-18-2004 06:14 AM |