368 Working with Movie Clips
Determining the depth of an instance
To determine the depth of a movie clip instance, use MovieClip.getDepth().
The following code iterates over all the movie clips on a SWF file’s main timeline and shows
each clip’s instance name and depth value in the Output panel:
for (var item:String in _root) {
var obj:Object = _root[item];
if (obj instanceof MovieClip) {
var objDepth:Number = obj.getDepth();
trace(obj._name + ":" + objDepth)
}
}
For more information, see getDepth (MovieClip.getDepth method) in the
ActionScript 2.0 Language Reference.
Swapping movie clip depths
To swap the depths of two movie clips on the same timeline, use MovieClip.swapDepths().
The following examples show how two movie clip instances can swap depths at runtime.
To swap movie clip depths:
1. Create a new Flash document called swap.fla.
2. Draw a blue circle on the Stage.
3. Select the blue circle, and then select Modify > Convert to Symbol.
4. Select the Movie clip option, and then click OK.
5. Select the instance on the Stage, and then type first_mc into the Instance Name text box
in the Property inspector.
6. Draw a red circle on the Stage, and then select Modify > Convert to Symbol.
7. Select the Movie clip option, and then click OK.
8. Select the instance on the Stage, and then type second_mc into the Instance Name text box
in the Property inspector.
9. Drag the two instances so that they overlap slightly on the Stage.
10. Select Frame 1 of the Timeline, and then type the following code into the Actions panel:
first_mc.onRelease = function() {
this.swapDepths(second_mc);
};
second_mc.onRelease = function() {
this.swapDepths(first_mc);
};