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:


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
tempSwapDepth = dragIcon.parent.getChildIndex(dragIcon);
var newSwapDepth:int = dragIcon.parent.getChildIndex(swapDepthMc);
newSwapDepth>oldSwapDepth ? newSwapDepth = newSwapDepth : newSwapDepth =  oldSwapDepth;
oldSwapDepth = dragIcon.parent.getChildIndex(dragIcon);
private function onMouseUp(event:MouseEvent):void         {
} */
private function onMouseDown(event:MouseEvent):void

    • atrangee
    • September 2nd, 2008

    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.

    • Cheyennemtnman
    • January 13th, 2010

    I have several elipses that expand on Mouse over and then display a box of text when Mouse done occurs.. I am having a problem of buttons are showing on top of the text box… when I want the Text box to be displayed on top of the buttons until mouse is released… Possible with the setChildIndex / AddChild() ?

    • Luca
    • May 27th, 2010

    Hi, I need help with this function:

    function dropIt(event:MouseEvent):void {
    var myTargetName:String = “target” + event.target.name;
    var myTarget:DisplayObject = getChildByName(myTargetName);
    cook_mc.filters = [];
    /*if (event.target.dropTarget.parent == myTarget){
    event.target.x = myTarget.x;
    event.target.y = myTarget.y;
    if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
    reply_txt.text = “Good Job!”;
    event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
    event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
    event.target.buttonMode = false;
    event.target.x = myTarget.x;
    event.target.y = myTarget.y;

    Insted of getChildByName(myTargetName) – I need whatever
    additional concrete names, for example (targetspeak_mc, targetlive_mc, targetplay_mc). Create more concrete targets for function drag and drop.

    There are objects and their targets on the stage. I need drag object and drop on target and function must show me correct answer (if I hit it).


  1. September 3rd, 2008

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: