Copy link to clipboard
Copied
Hello everyone,
I am trying to make drag & drop and I have 5 objects which need to be made draggable and droppable. What i want to do is make only two function, one for make all the items draggable when Mouse_DOWN on the objects respectively and one for make them drop when Mouse_UP. Is this possible using switch method. If anyone have idea then please help me. I want just two function rather than creating a individual function to make individual object drag and this way will create lots of functions.
Thanks.
Copy link to clipboard
Copied
You can write one mouse-down and one mouse-up function and then attach them to the objects that you want to use them in a loop. Something like this:
--------
var objectList:Array = new Array(objOneInstanceName, objOneInstanceName,objTwoInstanceName,objThreeInstanceName,objFourInstanceName,objFiveInstanceName);
for(var i in objectList) {
objectList.addEventListener(MouseEvent.MOUSE_DOWN,dragFunction);
objectList.addEventListener(MouseEvent.MOUSE_UP,dropFunction);
}
function dragFunction(Event:MouseEvent):void {
// your function here...
}
function dropFunction(Event:MouseEvent):void {
// your function here...
}
-------
Copy link to clipboard
Copied
Hey Rob Dillon thank you for the reply, here is the code that i applied:-
import flash.events.MouseEvent;
var objList:Array = new Array(ans_01, ans_02, ans_03);
for(var i in objList){
objList.addEventListener(MouseEvent.MOUSE_DOWN, startdrag);
objList.addEventListener(MouseEvent.MOUSE_UP, stopdrag);
}
function startdrag(e:MouseEvent):void{
objList.startDrag();
}
function stopdrag(e:MouseEvent):void{
objList.stopDrag();
}
What it is doing is only making the last item draggable stored in the last of the array.
Copy link to clipboard
Copied
maybe it should be
for (var i:int = 0; i<objList.length; i++)
Copy link to clipboard
Copied
This is another, slower, method to achieve the same result. In order to make your method actually useful you should define the value of objList.length and then use that. The way that this will execute as you have it written, the value of the length of the array will need to be calculated at every iteration through the loop. Since that value doesn't change, there is no good reason to calculate it over and over and over. So to get an actual useful version of your method it should read something like this:
----------
var objectList:Array = new Array(objOneInstanceName,objOneInstanceName,objTwoInstanceName,objThreeInstanceName,objFourInstanceName,objFiveInstanceName);
var thisMany:int = objectList.length;
for(var i:int = 0 ; i < thisMany ; i++) {
...
--------
Copy link to clipboard
Copied
You need to assign a value to the currentTarget object of the mouse event. Something like this:
function startdrag(e:MouseEvent):void {
var thisObj:Object = e.currentTarget;
thisObj.startDrag();
}
Copy link to clipboard
Copied
Hey thanks Rob Dillon, your method has done the trick for me..And thanks Rehex you too for your reply, i'll try yours also.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now