Copy link to clipboard
Copied
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!
as mentioned in message 1, you should be using currentTarget instead of target.
once your do that, what's the problem?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
what's the problem?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
show your code.
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
i put it in the comment above.
it works perfectly fine with 1 object and target, now i try to make it with 2
Copy link to clipboard
Copied
as mentioned in message 1, you should be using currentTarget instead of target.
once your do that, what's the problem?