Creating movie clips at runtime
Not only can you create movie clip instances in the Flash authoring environment, you can also
create movie clip instances at runtime in the following ways:
•
"Creating an empty movie clip" on page 211
•
"Duplicating or removing a movie clip" on page 212
•
"Attaching a movie clip symbol to the Stage" on page 212
Each movie clip instance you create at runtime must have an instance name and a depth
(stacking, or z-order) value. The depth you specify determines how the new clip overlaps with
other clips on the same Timeline. It also lets you overwrite movie clips that reside at the same
depth. (See
"Managing movie clip depths" on page
Creating an empty movie clip
To create a new, empty movie clip instance on the Stage, use the
method of the MovieClip class. This method creates a movie clip as a child of the clip that calls
the method. The registration point for a newly created empty movie clip is the upper left corner.
For example, the following code creates a new child movie clip named
the movie clip named
parent_mc.createEmptyMovieClip("new_mc", 10);
The following code creates a new movie clip named
file in which the script is run, and then invokes
into itself:
this.createEmptyMovieClip("canvas_mc", 10);
canvas_mc.loadMovie("flowers.jpg");
As shown in the following example, you can load the image picture.jpg into a movie clip and use
the
MovieClip.onPress()
replaces the movie clip with the image but doesn't give you access to movie clip
loadMovie()
methods. To get access to movie clip methods, you must create an empty parent movie clip and a
container child movie clip. Load the image into the container and place the event handler on the
parent movie clip.
//Creates a parent movie clip to hold the container
this.createEmptyMovieClip("mc_1", 0);
//creates a child movie clip inside of "mc_1"
//this is the movie clip the image will replace
mc_1.createEmptyMovieClip("container_mc",99);
//use moviecliploader to load the image
var my_mcl:MovieClipLoader = new MovieClipLoader();
my_mcl.loadClip("picture.jpg", mc_1.container_mc);
//put event handler on the parent movie clip mc_1
mc_1.onPress = function() {
trace("It works");
};
:
parent_mc
method to make the image act like a button. Loading a JPEG using
215.)
createEmptyMovieClip()
on the root Timeline of the SWF
canvas_mc
to load an external JPEG file
loadMovie()
Creating movie clips at runtime
at a depth of 10 in
new_mc
211
Need help?
Do you have a question about the FLASH MX 2004-USING ACTIONSCRIPT IN FLASH and is the answer not in the manual?