View comments | RSS feed

duplicateMovieClip (MovieClip.duplicateMovieClip method)

public duplicateMovieClip(name:String, depth:Number, [initObject:Object]) : MovieClip

Creates an instance of the specified movie clip while the SWF file is playing. Duplicated movie clips always start playing at Frame 1, no matter what frame the original movie clip is on when the duplicateMovieClip() method is called. Variables in the parent movie clip are not copied into the duplicate movie clip. Movie clips that are created with the duplicateMovieClip() method are not duplicated if you call the duplicateMovieClip() method on their parent. If the parent movie clip is deleted, the duplicate movie clip is also deleted. If you used MovieClip.loadMovie() or the MovieClipLoader class to load a movie clip, the contents of the SWF file are not duplicated. This means that you cannot save bandwidth by loading a JPEG, GIF, PNG, or SWF file and then duplicating the movie clip.

Contrast this method with the global function version of duplicateMovieClip(). The global version of this method requires a parameter that specifies the target movie clip to duplicate. Such a parameter is unnecessary for the MovieClip class version, because the target of the method is the movie clip instance on which the method is invoked. Moreover, the global version of duplicateMovieClip() supports neither the initobject parameter nor the return value of a reference to the newly created MovieClip instance.

Availability: ActionScript 1.0; Flash Player 5

Parameters

name:String - A unique identifier for the duplicate movie clip.

depth:Number - A unique integer specifying the depth at which the new movie clip is placed. Use depth -16384 to place the new movie clip instance beneath all content that is created in the authoring environment. Values between -16383 and -1, inclusive, are reserved for use by the authoring environment and should not be used with this method. The remaining valid depth values range from 0 to 1048575, inclusive.

initObject:Object [optional] - (Supported for Flash Player 6 and later.) An object that contains properties with which to populate the duplicated movie clip. This parameter allows dynamically created movie clips to receive clip parameters. If initObject is not an object, it is ignored. All properties of initObject are copied into the new instance. The properties specified with initObject are available to the constructor function.

Returns

MovieClip - A reference to the duplicated movie clip (supported for Flash Player 6 and later).

Example

The following example duplicates a newly created MovieClip a number of times and traces the target for each duplicate.

var container:MovieClip = setUpContainer();
var ln:Number = 10;
var spacer:Number = 1;
var duplicate:MovieClip;
for(var i:Number = 1; i < ln; i++) {
    var newY:Number = i * (container._height + spacer);
    duplicate = container.duplicateMovieClip("clip-" + i, i, {_y:newY});
    trace(duplicate); // _level0.clip-[number]
}

function setUpContainer():MovieClip {
    var mc:MovieClip = this.createEmptyMovieClip("container", this.getNextHighestDepth());
    var w:Number = 100;
    var h:Number = 20;
    mc.beginFill(0x333333);
    mc.lineTo(w, 0);
    mc.lineTo(w, h);
    mc.lineTo(0, h);
    mc.lineTo(0, 0);
    mc.endFill();
    return mc;
}

The MovieClip.getNextHighestDepth() method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components DepthManager class instead of the MovieClip.getNextHighestDepth() method.

See also

loadMovie (MovieClip.loadMovie method), removeMovieClip (MovieClip.removeMovieClip method), duplicateMovieClip function


Version 8

Comments


DblS said on Nov 21, 2005 at 11:04 AM :
Is there a way to duplicate Textfields and assign dynamic content to them in AS? I know that having one in your library with a linkage name works, but I'm wondering about having it all dynamically created.
peterd_mm said on Dec 5, 2005 at 4:49 PM :
DblS,

Have you tried nesting the text field within a Movie clip container and calling MovieClip.duplicateMovieClip(...)?

_peter
No screen name said on Dec 30, 2005 at 2:17 PM :
this code should solve your problem.
[CODE]


drawBox();
function drawBox() {
var box:MovieClip = this.createEmptyMovieClip("box"+i, this.getNextHighestDepth());
box.beginFill(0xffff00, 100);
box.lineStyle(1, 0x0, 100);
box.moveTo(0, 0);
box.lineTo(0, 30);
box.lineTo(200, 30);
box.lineTo(200, 0);
box.lineTo(0, 0);
box._x = 0;
// set start position of this box
box._y = 0;
addLetter(box);
}
function addLetter(target:MovieClip) {
// create the text box
target.createTextField("letter", target.getNextHighestDepth(), 0, 0, 200, 20);
// posistion it centered to the box
target.letter._x += 10;
target.letter._y -= 5;
// get the text
target.letter.text = "change this to a var or plain text";
}
[/CODE]
William_Donelson said on Sep 29, 2006 at 1:00 AM :
Suppose I have a SWF loaded which has an embedded font in it.

If I call duplicateMovie( ) to make a copy of the SWF, will the FONT DATA be copied as well ? (e.g. if I have a 300KB font embedded, is a copy made of that font data?)

Thanks
Paazio said on Jan 24, 2007 at 12:34 AM :
Documentation states that "If you used MovieClip.loadMovie() or the MovieClipLoader class to load a movie clip, the contents of the SWF file are not duplicated".

How about attachMovieClip ?
The same affect, not duplicated?

Any suggestions for a way to save bandwidth while using just one loaded image many times?

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/8/main/00002452.html