Skip to main content
Known Participant
January 4, 2015
Question

make multiple items dragabble

  • January 4, 2015
  • 1 reply
  • 504 views

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.

This topic has been closed for replies.

1 reply

robdillon
Participating Frequently
January 4, 2015

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...

}

-------

Pop04Author
Known Participant
January 5, 2015

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.

Known Participant
January 5, 2015

maybe it should be

for (var i:int = 0; i<objList.length; i++)