Flash: Using swapDepths()

NOTE: This article is only valid for ActionScript 2. In ActionScript 3, movieclip depths start at 0 and are contiguous. If you have 2 objects within a container, they will be at depths 0 and 1, respectively. You cannot have one at depth 0 and the other at 3 leaving 2 empty depths (depths 1 and 2).

So you want to change the depth of your pictures and text in Flash with ActionScript?

Check out this tutorial, Depths – How they work in Flash, at Kirupa.com. The first page you should already know if you been doing coding ActionScript professionally for some time. It is the second page that is the most interesting:

Major points of the second page:

  • Timeline Zone: Flash places static items on the timeline in depths -16,384 to -1.
  • Dynamic Zone: Dynamically placed movieclips are in 0 to 1,048,575 (and can only be removed from this range)
  • Reserved Zone: Dynamically placed movieclips can be in 1,048,576 to 2,130,690,04, (but can’t be removed)
  • The exception to these zones is createEmptyMovieClip method. There is no limit for the depth when creating an empty movieclip.
  • The timeline refreshes when gotoAndPlay() is called. Timeline refreshes do not keep dynamically placed movieclips on the screen if they were placed there by a frame in the future of the timeline. Example: A movieclip is dynamically created (or swapDepths is called, placing the movieclip in the “Dynamic Zone”) in frame 10. A refresh occurs in frame 5, when gotoAndPlay(5) is called. The dynamically created movieclip (from frame 10) dissappears.
  • If you use swapDepths to bring a movieclip which was placed on the timeline originally to a depth above 0 (not the Timeline Zone), then in a timeline refresh, that clip will not be removed in the clearing of the depths (the refresh) and a new instance of the same movieclip will be placed at its original depth, thus duplicating it and causing runtime errors that Flash Player will not flag.

So what is one to do? If you just swapping movieclips back and forth, like in this template for the Renaissance Pointe, then call swapDepths() with a negative value.

Looking for an Actionscript 3 tutorial?

You might try this tutorial on swapDepths for Actionscript 3.

2 Responses to “Flash: Using swapDepths()”

  1. Dominic says:

    I’m still unclear on how swapDepths works if the MCs trying to be swapped were created from AS. For example, I use a for loop to create a number of MCs based on city locations as well as create the onRollOver feature of those MCs. I’ve named the clips dynamically in the same loop, all of which are contained within a MC created using createNewMovieClip and attachMovie.
    If I try to include the swapping of depths using swapDepth, nothing happens. No errors and certainly no swapping of depths. But if I take the same MC and place it on the stage physically, the code works. It seems to be because I’m not referencing the MCs dynamically and can actually hard code names. This is obviously not the ideal solution.
    What do you suuggest?
    Here is the function I’m using to make all this happen.
    // Function: fPlaceLocDots()
    // Descrip: Places the appropriate number of locations on the map.
    function fPlaceLocDots(nNumLocs) {
    //trace(“Build ” + nNumLocs + ” CITY buttons.”);
    //trace(“fPlaceLocDots: ” + this._parent);
    var aTempDestArray:Array;
    var strNameToPass:String;
    var nUniqueID:Number;
    for(var btns:Number = 0; btns < nNumLocs; btns++) {
    var strTempName:String = _root.objContentHolder.aRouteMapContentList[btns].strName; // Store the instance name.
    strNameToPass = strTempName;
    var strTempLoc:String = _root.objContentHolder.aRouteMapContentList[btns].strCoordLoc; // Temporarily store the instance coordinates.
    var aTempLoc:Array = strTempLoc.split(“,”); // Store the instance coordinates in an array.
    var strTempAircraftType:String = _root.objContentHolder.aRouteMapContentList[btns].strAircraftType; // Temporarily store the instance aircraft type.
    var aTempAircraftType:Array = strTempAircraftType.split(“,”); // Store the instance aircraft types in an array.
    var strTempDestArray:String = _root.objContentHolder.aRouteMapContentList[btns].strDestinationsServed; // Temporarily store the instance destinations.
    aTempDestArray = strTempDestArray.split(“,”); // Store the instance destinations in an array.
    var nTempX:Number = aTempLoc[0]; // Store the instance X location.
    var nTempY:Number = aTempLoc[1]; // Store the instance Y location.

    /* for (var acType:Number = 0; acType 3) {
    if (_root.objContentHolder.aRouteMapContentList[btns].strAircraftType.length > 7) {
    //trace(” 3 acType”);
    fCreateRouteLayer(strNameToPass, aTempAircraftType[acType]);
    } else {
    //trace(” 2 acType”);
    fCreateRouteLayer(strNameToPass, aTempAircraftType[acType]);
    } else {
    //trace(” 1 acType”);
    fCreateRouteLayer(strNameToPass, aTempAircraftType[acType]);
    var routes777:MovieClip = this.createEmptyMovieClip(“777routes”, this.getNextHighestDepth()); // Create a movieClip to hold the 777 markers.
    routes777.attachMovie(“at777Dot_mc”, strTempName, -100 + btns); // Attach a 777 button for each instance.
    routes777[strTempName]._x = nTempX; // Store the button instance X coordinate.
    routes777[strTempName]._y = nTempY; // Store the button instance Y coordinate.
    routes777[strTempName].destinations = aTempDestArray; // Store the button instance destinations.
    nUniqueID = btns;
    routes777[strTempName].uniqueID = nUniqueID;
    routes777[strTempName].locationMarkerName.htmlText = _root.objContentHolder.aRouteMapContentList[btns].strName // Name the button instance with this location name.
    // Assign the button features for each instance.
    routes777[strTempName].locationMarkerGrfx.onRollOver = function() {
    trace(“PARENT MC: ” + this._parent);
    trace(“parent depth: ” + this._parent.getDepth());
    trace(“grand parent: ” + this._parent._parent);
    trace(“grand parent depth: ” + this._parent._parent.getDepth());
    trace(“Destinations: ” + this._parent.destinations);
    trace(“nUniqueID: ” + this._parent.uniqueID);
    trace(“Revenue Type: ” + _root.objContentHolder.aRouteMapContentList[this._parent.uniqueID].strRevenueType);
    trace(“this name: ” + this);
    trace(“this depth: ” + this.getDepth());
    trace(“new grand parent depth: ” + this._parent._parent.getDepth());
    trace(“*************************” + newline + “INFORMATION TO DISPLAY: ” + newline + _root.objContentHolder.aRouteMapContentList[this._parent.uniqueID].strBlurb + newline + “*************************”);
    _root.outputBOX_mc._visible = true;
    _root.outputBOX_mc.outputTEXT.text = _root.objContentHolder.aRouteMapContentList[this._parent.uniqueID].strBlurb;
    var outputText:TextFormat = new TextFormat();
    with (outputText) {
    font = “Arial”;
    size = 10.0;
    color = 0×333333;
    //bold = true;
    _root.outputBOX_mc.outputTEXT.wordWrap = true;
    _root.outputBOX_mc.outputTEXT.autoSize = “left”;
    var x0:Number = this._parent._x; // X START point
    var y0:Number = this._parent._y; // Y START point
    //HOUSTON loc: 388.3, 173.3
    var x2:Number = 388.3; // X END point
    var y2:Number = 173.3; // Y END point
    this._parent.textBG._visible = true;
    // This 4loop is for debugging to identify which destinatiosn to draw.
    for (var tempDestArray:Number = 0; tempDestArray < this._parent.destinations.length; tempDestArray++) {
    //trace(“dest” + “[" + tda + "]: ” + this._parent.destinations[tda]);
    // Pass the current array of destinations to draw arcs to.
    fDrawPaths(x0, y0, x2, y2, this._parent.destinations);
    routes777[strTempName].locationMarkerGrfx.onRollOut = function():Void {
    this._parent.textBG._visible = false;
    _root.outputBOX_mc._visible = false;
    _root.outputBOX_mc.outputTEXT.text = “Roll over a city for details.”;
    var outputText:TextFormat = new TextFormat();
    with (outputText) {
    font = “Arial”;
    size = 10.0;
    color = 0×333333;
    //bold = true;
    _root.outputBOX_mc.outputTEXT.wordWrap = true;
    _root.outputBOX_mc.outputTEXT.autoSize = “left”;

    Any help is greatly appreciated!

  2. smjdesign says:

    @Dominic Umm… that’s a lot of code, that I don’t have access to to test. My first instinct is if you are creating dynamic movieclips (by your code) then you are placing them on a depth starting with 1 and increasing. Your code appears to be subtracting. This would cause a collision between the current movieclip on a depth of n and the one on a depth n-1. Shouldn’t it be “swapDepths(+1);”

Leave a Reply