7.16. Changing Stacking Order

Problem

You want to swap the depths of two movie clips.

Solution

Use the swapDepths( ) method.

Discussion

The swapDepths( ) method swaps the depths of any two movie clips. You should call it from one of the two movie clips and pass it a reference to the other:

myMovieClip1.swapDepths(myMovieClip2);

One way in which this method is particularly useful is in swapping the depths of two movie clips when hitTest( ) detects that one overlaps the other. The following example uses this concept but also illustrates the problem that can occur when trying to use this technique.

Warning

The following example shows a white circle and a black circle that overlap and rapidly change depths. The result is a very fast blinking effect, which may be induce seizures in readers prone to epilepsy or may induce nausea in sensitive viewers. Please exercise caution.

The following code swaps two clips’ depths when they overlap:

// Include DrawingMethods.as from Chapter 4 for its drawCircle(  ) method. #include "DrawingMethods.as" // Draw two circle movie clips: one black and one white. _root.createEmptyMovieClip("circle1", 1); circle1.lineStyle(1, 0x000000, 100); circle1.beginFill(0xFFFFFF, 100); circle1.drawCircle(100); circle1.endFill( ); _root.createEmptyMovieClip("circle2", 2); circle2.lineStyle(1, 0x000000, 100); circle2.beginFill(0, 100); circle2.drawCircle(100); circle2.endFill( ); // Set each movie clip to be on the visible part of the Stage. circle1._x = circle2._x = 200; circle1._y ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.