# Function to duplicate movie

**URL:** <https://forum.kirupa.com/t/function-to-duplicate-movie/29777>\
**Category:** Uncategorized\
**Created:** [August 30, 2003, 6:37pm UTC](https://forum.kirupa.com/t/function-to-duplicate-movie/29777 "2003-08-30T18:37:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hoppedup](https://avatars.discourse-cdn.com/v4/letter/h/dfb087/32.png) [@hoppedup](https://forum.kirupa.com/u/hoppedup)\
**Post date:** [August 30, 2003, 6:37pm UTC](https://forum.kirupa.com/t/function-to-duplicate-movie/29777/1 "2003-08-30T18:37:30Z")

</div>

so i want to duplicate a movie multiple times. each duplicate i would have to then set some properties for, such as a random location and change the contents of the textbox it contains.

so i thought writing a function would be an easy way to accomplish this. so this is what i tried:

-i is the depth, just to make each movie lay on top of the previous ones.  
-newName is the new instance name for the box  
-chem is the string that will be entered into the textbox  
-it duplicates the movie txt1  
[AS]  
function dupe(newName, chem){  
i++;  
dulicateMovieClip(\_root.txt1,newName,i);  
\_root[newName].\_x = random(x);  
\_root[newName].\_y = random(y);  
\_root[newName].htmlText = “\<font face=“Verdana” size=”+3"\>" + chem + “\</font\>”;  
}  
[/AS]

so i try to later call the function with  
[AS]  
dupe(“text2”, “radon”);  
[/AS]

but it doesn’t work. am i not specifying the new instance name correctly by using ‘\_root[newName]’? or is it something else?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 30, 2003, 7:02pm UTC](https://forum.kirupa.com/t/function-to-duplicate-movie/29777/2 "2003-08-30T19:02:49Z")

</div>

Have you defined i, x, and y ?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 30, 2003, 7:16pm UTC](https://forum.kirupa.com/t/function-to-duplicate-movie/29777/3 "2003-08-30T19:16:49Z")

</div>

yeah and if you’re with mx use Math.random()\*x or Math.random()\*y

hope it’s not because of the missing ‘p’ in du **p** licate.

and i is supposed to be a movie clip

> 

MovieClip.duplicateMovieClip

Availability

Flash Player 5.

Usage

myMovieClip.duplicateMovieClip(newname, depth [,initObject])

Parameters

newname A unique identifier for the duplicate movie clip.

depth A unique number specifying the depth at which the movie specified is to be placed.

initObject An object containing 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. This parameter is optional.

Returns

Nothing.

Description

Method; creates an instance of the specified movie clip while the movie 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 have been created using the duplicateMovieClip method are not duplicated if you call the duplicateMovieMethod on their parent. If the parent movie clip is deleted the duplicate movie clip is also deleted. Movie clips added with duplicateMovieClip can be deleted with removeMovieClip action or method.

See also

duplicateMovieClip  
MovieClip.removeMovieClip  
removeMovieClip

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 30, 2003, 7:26pm UTC](https://forum.kirupa.com/t/function-to-duplicate-movie/29777/4 "2003-08-30T19:26:09Z")

</div>

yeah it was because of the p. thats kind of sad… lol

and i was using duplicateMovieClip(target, newname,depth)… i guess the older version of the function

> 

duplicateMovieClip

Availability

Flash Player 4.

Usage

duplicateMovieClip(target, newname,depth)

… and so on

and the only difference between Math.random()\*x and random(x) is that the former allows for decimals, right? or is it a better function for generating random numbers?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 30, 2003, 8:00pm UTC](https://forum.kirupa.com/t/function-to-duplicate-movie/29777/5 "2003-08-30T20:00:51Z")

</div>

There’s another problem here…

Are you trying to duplicate the TextField, or the MovieClip that contains the TextField?

1. If you want to duplicate the TextField, you’ll need to define a method to do so. A quick guess:  
[AS]TextField.prototype.duplicateTextField = function(newName, depth) {  
if (arguments.length\<2) return;  
this.\_parent.createTextField(newName, depth, 0, 0, 0, 0);  
for (var prop in this) {  
this.\_parent[newName][prop] = this[prop];  
}  
return this.\_parent[newName];  
};[/AS]
2. If you want to duplicate the MovieClip that contains the TextField, this line is wrong:  
[AS]\_root[newName].htmlText = “\<font face=“Verdana” size=”+3"\>"+chem+"\</font\>";[/AS]  
It should read:  
[AS]\_root[newName].instance.htmlText = “\<font face=“Verdana” size=”+3"\>"+chem+"\</font\>";[/AS]  
Where _instance_ is the instance name of the TextField inside the MovieClip.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 30, 2003, 9:18pm UTC](https://forum.kirupa.com/t/function-to-duplicate-movie/29777/6 "2003-08-30T21:18:18Z")

</div>

createTextField? I didn’t even knew that existed!! You amaze me everytime you post _Kode_.

Just to throw in my two coins; I don’t know about the text fields, but I put the duplicate command in a function, then all the atributes of whatever I’m duplicating on the movieClip to be duplicated, then it will duplicate the code as well.

This isn’t the best way to do it, though; I’ve been told that it’s disorganized and sloppy; but I’m lazy and find that way easier - Kode will disagree (I remember :P) - good luck!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 30, 2003, 9:21pm UTC](https://forum.kirupa.com/t/function-to-duplicate-movie/29777/7 "2003-08-30T21:21:12Z")

</div>

Quick question ka_sorry_kode:

[AS]if (arguments.length\<2)[/AS]

Is arguments a keyword that indicates the amount of arguments the function received ? And what exactly do these lines do ?

[AS]  
for (var prop in this) {  
this.\_parent[newName][prop] = this[prop];  
}  
[/AS]

I’m not familiar with for whatever in something …

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 30, 2003, 9:30pm UTC](https://forum.kirupa.com/t/function-to-duplicate-movie/29777/8 "2003-08-30T21:30:30Z")

</div>

Read about the Arguments object here: [http://www.macromedia.com/support/flash/action\_scripts/actionscript\_dictionary/actionscript\_dictionary055.html](http://www.macromedia.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary055.html).

And the code basically loops through the properties of the TextField, and sets the properties of the newly created TextField equal to the properties of the TextField you want to duplicate. Am I making any sense? 😛
