Flash Script – Swapping Images in Flash

Georgina Laidlaw
Share


This tutorial shows you how to use the swapDepths function.

Think of a scenario where you have 3 pictures aligned one on top of the other, and you want to move the picture that underlies the top image. There are many ways to do this in Flash, and here, we’ll use the swapDepths feature.

Download the sample files here.

1. In the above example I’ve used three rectangular movielips. I have given each one instance names: "red", "blue", and "green".

2. These are the actions I’ve applied to each button:

Blue button:

on (press){_root.blue.swapDepths(1);}

Red button:

on (press){_root.red.swapDepths(1);}

Green button:

on (press){_root.green.swapDepths(1);}

Usage:

myMovieClip.swapDepths(depth) 
myMovieClip.swapDepths(target)

Parameters:

target

This refers to the instance of the movie clip whose depth is swapped by the instance specified in myMovieClip. Both instances must have the same parent movie clip.

depth

This refers to the number specifying the depth level where MovieClip is to be placed.

This swaps the stacking, or z order (depth level), of the specified instance (MovieClip) with either:

  • the movie that’s specified by the target parameter, or
  • the movie that currently occupies the depth level specified in the depth parameter.

Both movies must have the same parent movie clip. Swapping the depth level of movie clips has the effect of moving one movie in front of, or behind, the other. And if a movie is tweening when this method is called, the tweening is stopped.