User Guide

38 Chapter 2: ActionScript Basics
my_mc.startDrag(true);
parent_mc.getURL("http://www.macromedia.com/support/" + product);
The second example returns the width of a movie clip called my_mc on the Stage. The targeted
instance must be a movie clip, and the returned value must be a numeric value.
function getMCWidth(target_mc:MovieClip):Number {
return target_mc._width;
}
trace(getMCWidth(my_mc));
Creating movie clips dynamically
Using ActionScript to create movie clips dynamically is
useful when you want to avoid manually creating movie clips on the Stage or attaching them from
the library. For example, you might create an image gallery with a large number of thumbnails
that you want to organize on the Stage. Using
MovieClip.createEmptyMovieClip() lets you
create an application entirely using ActionScript.
To dynamically create a movie clip, use
MovieClip.createEmptyMovieClip(), as shown in the
following example:
//Creates a movie clip to hold the container
this.createEmptyMovieClip("image_mc", 9);
//loads an image into image_mc
image_mc.loadMovie("picture.jpg");
The second example creates a movie clip called square_mc that uses the Drawing API to draw a
rectangle. Event handlers and the
startDrag() and stopDrag() methods of the MovieClip class
are added to make the rectangle draggable.
this.createEmptyMovieClip("square_mc", 1);
square_mc.lineStyle(1, 0x000000, 100);
square_mc.beginFill(0xFF0000, 100);
square_mc.moveTo(100, 100);
square_mc.lineTo(200, 100);
square_mc.lineTo(200, 200);
square_mc.lineTo(100, 200);
square_mc.lineTo(100, 100);
square_mc.endFill();
square_mc.onPress = function() {
this.startDrag();
};
square_mc.onRelease = function() {
this.stopDrag();
};
For more information, see “Working with Movie Clips” on page 205 and the “MovieClip class
entry in Flash ActionScript Language Reference.