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:
// IncludeDrawingMethods.asfrom Chapter 4 for itsdrawCircle( )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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access