Function to duplicate movie

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?

Have you defined i, x, and y ?

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 duplicate.

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

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?

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.

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!

Quick question kasorrykode:

[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 …

Read about the Arguments object here: 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? :stuck_out_tongue: