swapDepth in AS3 for drag n drop

The swapDepth is not available on AS3, rather much more simplest way is available. :)

The below commented code can be achieved through the single line:

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

private var _dragIcon:MovieClip;
dragIcon.addEventListener(MouseEvent.MOUSE_UP,onMouseUp, false, 0, true);
dragIcon.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown, false, 0, true);

/*
private var oldSwapDepth:int = 0;
private var tempSwapDepth:int = 0;
private function onMouseDown(event:MouseEvent):void
{
dragIcon.startDrag(false)
tempSwapDepth = dragIcon.parent.getChildIndex(dragIcon);
var newSwapDepth:int = dragIcon.parent.getChildIndex(swapDepthMc);
newSwapDepth>oldSwapDepth ? newSwapDepth = newSwapDepth : newSwapDepth =  oldSwapDepth;
dragIcon.parent.setChildIndex(dragIcon,newSwapDepth)
oldSwapDepth = dragIcon.parent.getChildIndex(dragIcon);
}
private function onMouseUp(event:MouseEvent):void         {
dragIcon.stopDrag();
dragIcon.parent.setChildIndex(dragIcon,tempSwapDepth);
} */
private function onMouseDown(event:MouseEvent):void
{
event.target.parent.addChild(event.target);
}

2 Responses to “swapDepth in AS3 for drag n drop”

  1. atrangee Says:

    Well,
    you can also use setChildIndex()
    I had some problems in Flex when using addChild() again and again, and I found that setChildIndex works properly in those cases.

  2. Flash tutorials | AS3 Drag and Drop | Lemlinh.com Says:

    [...] Read more [...]

Leave a Reply