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

multiple "drag and drop"

New Here ,
Sep 19, 2018 Sep 19, 2018

HI, i have a problem with drag and drop programming in animate actionscript 3

i have 2 movieclips "red1" and "red2"

and the goal is to drag any of them to the targerts "target1" and "target2" that

the specific drag of the movieclip to its target does not matter (red 1 can be dragged to target2, and vice versa)

how do i do that?

thank you!

329
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

correct answers 1 Correct answer

Community Expert , Sep 20, 2018 Sep 20, 2018

as mentioned in message 1, you should be using currentTarget instead of target.

once your do that, what's the problem?

Translate
Community Expert ,
Sep 19, 2018 Sep 19, 2018

add mousedown and mouseup listeners to both

in the mousedown listener start a loop that assigns the currentTarget's x and y properties to mouseX and mouseY.

in the mouse up listener terminate the loop and check for a hit with your drop target.

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
New Here ,
Sep 19, 2018 Sep 19, 2018

kglad yes that part i understood, i have sucsessfully created drag and drop with one object and one target (when object dropped on target, it stays, and when object dropped anywhere else, it returns to original position)

now i try to do the same with 2 identical objects, and 2 targets, and for my purpose it shouldn't matter what object i drag to what target, when i drag the object to any target, it should stay there, and then drag the second object to the remaining target.

thank you

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 Expert ,
Sep 19, 2018 Sep 19, 2018

what's the problem?

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
New Here ,
Sep 19, 2018 Sep 19, 2018

kglad, I do not know how to make the drag and drop with 2 objects & targets, only with single object and drop target.

when its object -------> target, its fine.

when its object 1 and object 2 --------> target 1 and 2, it doesnt works.

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 Expert ,
Sep 19, 2018 Sep 19, 2018

show your code.

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
New Here ,
Sep 19, 2018 Sep 19, 2018

var objectoriginalX:Number;

var objectoriginalY:Number;

function startdragging (event:MouseEvent):void

{

event.target.startDrag();

event.target.parent.addChild(event.target);

//trace(event.target.parent);

objectoriginalX = event.target.x;

    objectoriginalY = event.target.y;

}

function stopdragging (event:MouseEvent):void

{

event.target.stopDrag();

var droptargetname:String = "target" + event.target.name;

var droptarget:DisplayObject = getChildByName(droptargetname);

var targetcenter:Number;

var droptargetcenter:Number;

targetcenter = (event.target.x + event.target.width) / 2;

droptargetcenter = (droptarget.x + droptarget.width) / 2;

if ((event.target.dropTarget != null) && (event.target.dropTarget.parent == droptarget))

{

//trace(event.target.dropTarget.parent.x);

event.target.removeEventListener(MouseEvent.MOUSE_DOWN, startdragging);

        event.target.removeEventListener(MouseEvent.MOUSE_UP, stopdragging);

event.target.x = droptarget.x

event.target.y = droptarget.y

}

else

{

    event.target.x = objectoriginalX;

    event.target.y = objectoriginalY;

}

}

red.addEventListener(MouseEvent.MOUSE_DOWN, startdragging);

red.addEventListener(MouseEvent.MOUSE_UP, stopdragging);

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
New Here ,
Sep 19, 2018 Sep 19, 2018

i put it in the comment above.

it works perfectly fine with 1 object and target, now i try to make it with 2

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 Expert ,
Sep 20, 2018 Sep 20, 2018
LATEST

as mentioned in message 1, you should be using currentTarget instead of target.

once your do that, what's the problem?

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