Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

make multiple items dragabble

Community Beginner ,
Jan 04, 2015 Jan 04, 2015

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.

TOPICS
ActionScript
420
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 04, 2015 Jan 04, 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...

}

-------

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 05, 2015 Jan 05, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 05, 2015 Jan 05, 2015

maybe it should be

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 05, 2015 Jan 05, 2015
LATEST

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++) {

...

--------

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 05, 2015 Jan 05, 2015

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();

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 05, 2015 Jan 05, 2015

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines