11.8. Moving a Movie Clip in Front of or Behind Another Movie Clip (or a Button or Text Field)

Problem

You want to move a movie clip in front of or behind another movie clip, button, or text field.

Solution

Use layers or the arrangement commands to adjust the order of instances at authoring time.

Use the swapDepths( ) method to change the stacking order of any two instances at runtime.

Discussion

When you are working with multiple movie clip, button, and/or text field instances on the stage at the same time, you need to be aware of their stacking order. For example, if you have a movie clip that is supposed to animate behind a text field, you need to make sure that the text field appears in front of the movie clip. You can make these kinds of adjustments at authoring time either by placing the instances on different layers and arranging the layer order or by changing the order using the arrangement commands. You can move an instance forward or back by selecting the instance and then choosing Modify → Arrange → Bring Forward/Send Back/Bring to Front/Send to Back.

The authoring time techniques work just fine when you want to maintain a constant stacking order of all the instances in the movie. But when you want the order to change, you need to use ActionScript to effect that change at runtime using the swapDepths( ) method. In some cases, it is important that you be able to programmatically change the depths of instances in this way. For example, if you create a puzzle in which the user ...

Get Flash 8 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.