How to Swap Depth using AS3 via setChildIndex

How to swap depth using AS3 via setChildIndex. This will fix the Warning: 1060: Migration issue: The method getNextHighestDepth is no longer supported.

For reference, here  is how depth management was done using AS2:

mc.onRollOver = function():Void
{
// move current movie clip forward to the next highest depth
this.swapDepths(this._parent.getNextHighestDepth());
}

Here’s how to swap depth in Actionscript 3.

Use the setChildIndex method to changes the position of an existing child in the display object container with the instances name “holder_mc”. This affects the layering of child objects. This example used below attaches a movie clip from the library three times. That movie clip Linkage class is named “MyMC”. Inside of that movie clip is a nested movie clip not a graphic shape. I made sure to use a nested movie clip so we could dynamically attach variable and target them later via the mouse event.

// loop through to create three mc's on stage
for (var i:int = 0; i <3; i++) {
var myColor:ColorTransform = new ColorTransform();
myColor.color = (Math.random() * 0xFFFFFF);
var mc:MovieClip = new MyMC();
mc.x = ((i * 80) + mc.width);
mc.y = 100;
mc.id = i;
mc.name = "b"+i;
mc.transform.colorTransform = myColor;
mc.buttonMode = true;
mc.addEventListener (MouseEvent.MOUSE_OVER, swapDepths);
holder_mc.addChild (mc);
}

function swapDepths (e:MouseEvent):void {
var mc:MovieClip;
mc = e.target.parent;
trace (mc.id);
holder_mc.setChildIndex (holder_mc.getChildByName("b"+mc.id), (holder_mc.numChildren - 1));
}

The most important part above is the setChildIndex referencing the current child by name (getChildByName) and finding the highest index number (numChildren-1) for placement.

6 thoughts on “How to Swap Depth using AS3 via setChildIndex

  1. I’m trying to enter a depth but I get this error:
    RangeError: Error #2006: The supplied index is out of bounds.
    at flash.display::DisplayObjectContainer/setChildIndex()
    at soundSpectrum_fla::MainTimeline/onLoaded()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

  2. didnt seem to work for me. i altered it to correct the problem:
    /////////
    // loop through to create three mc’s on stage
    for (var i:int = 0; i <3; i++) {
    var myColor:ColorTransform = new ColorTransform();
    myColor.color = (Math.random() * 0xFFFFFF);
    var mc:MovieClip = new MyMC();
    mc.x = ((i * 80) );
    mc.y = 100+ (i*10);
    mc.id = i;
    mc.name = "b"+i;
    mc.transform.colorTransform = myColor;
    mc.buttonMode = true;
    mc.addEventListener (MouseEvent.MOUSE_OVER, swapDepths);
    holder_mc.addChild (mc);
    }

    function swapDepths (e:MouseEvent):void {
    trace (e.target.id);
    var mc:MovieClip;
    mc = (e.target.parent as MovieClip);
    trace (mc.id);
    holder_mc.setChildIndex (holder_mc.getChildByName("b"+e.target.id), (holder_mc.numChildren – 1));
    }

Leave a Reply

Your email address will not be published. Required fields are marked *