[h1]
Duplicate Movieclip Tutorial[/h1]
Difficulty:
Moderate –
Expert
Need to know-
- What a variable is
- How to create, modify, and add an instance name to a Movieclip
- Basic action script statements, such as, if, else if, onClipEvent
- Have Flash 8 to download the source.fla, and have FlashPlayer 8 to view the final product.
1 Flash has the ability to copy a Movieclip, and give it it’s own unique name and depth. The script to do this is duplicateMovieClip(parent Movieclip, name, depth). I’ll quickly go over why the depth is important, then walk you through on how to use the script.
In Flash, everything is assigned its own unique depth. It can be any real number, but it has to be unique! If two Movieclips exist on the same depth, the old one is removed, and the new one stays. To change the depth of a Movieclip at any time, simply add this action to it:
onClipEvent(load){
this.SwapDepths(depth)
}
Just replace the depth with whatever number you want.
*Note* A Movieclip with a higher depth will overlap one with a lower depth.
I will show you how to use the duplicateMovieClip() function to have a dot leave a trail of red dots.
*Note* If you have download and finished my
Full Range Movement tutorial, you can open it and skip to section 2! If you haven’t, please take the tutorial, or download the source (.fla).
2 Okay, we need to add two new Movieclips, and put them off the main frame. First, make any Movieclip, it can be ANYTHING! No one will see it, and it’s just to add some actions to it, but, just for the sake of this tutorial, make a 10x10 box and a 2x2 circle like these:

Next, convert them both into their own separate movie clips:
The box:

And the 2x2 dot:

3 Now that you have all three Movieclips, put the Trail Movieclip and the DM Movieclip off the main stage (out of sight) and add these instance names to the respective Movieclips, without the ‘s:
DM – ‘dupe’
Trail – ‘trail’
Hero – ‘hero’
4 Now that we have all the Movieclips set up, let’s add the scripts!
For DM –
onClipEvent (load) {
i = 0;
time = 0;
}
onClipEvent (enterFrame) {
if (time>0) {
time--;
} else {
duplicateMovieClip(_root.trail, "t"+i, i);
i++;
time = 5;
}
}
Okay, that’s a little confusing, seeing as how I jumped right into the duplicateMovieClip(), so let me explain that step by step.
onClipEvent (load) {
i = 0;
time = 0;
}
As soon as the Movieclip loads, it sets two variables. The ‘i’ variable is the universal variable for imaginary number, I’ll tie back into this later. And the ‘time’ variable, that’s so we don’t flood the flash with duplicates!
onClipEvent (enterFrame) {
if (time>0) {
time--;
} else {
duplicateMovieClip(_root.trail, "t"+i, i);
i++;
time = 5;
}
}
This is the bulk of the code, so let me explain this:
As long as this Movieclip (dupe) is on the frame, and if time is > then 0, then decrement time by 1. Else, if time isn’t > 0, then duplicate the Movieclip(trail) and re-name it ‘t’+I, so if i = 0, then it’d be t0, if ‘I’ = 1, then it’d be t1. And set the depth of the new Movieclip(‘t’+i) to ‘i’ then, increment ‘i’ by 1, and set ‘time’ to 5.
Confused? Good, here’s the easy version:
Make a copy of “trail”, give it it’s own unique name and depth, then increment that variable by one, creating a nice little loop!
For Trail-
onClipEvent (load) {
_root.trail._x = -50;
_root.trail._y = -50;
_x = _root.hero._x;
_y = _root.hero._y;
}
This is an easy, but strange one! What it’s doing, it making the Trail Movieclip’s _x and _y -50, but at the same time, making it the same as the ‘hero’ Movieclip! Why does this work? Because the duplicateMovieClip() code also copies actions! So, when the new Movieclip is made, it down’t have the instance name ‘trail’ anymore, so this keeps trail out of site!
That’s it! You’re done! Here’s what I got as my final product; use W A S D to move:
http://www.graphicaddicts.net/ssj/Duplicate%20Movie%20Clip.swf
If you like this tutorial, please
Click Here to register.
Click the link below to download the Flash file for this tutorial.
Duplicate Movieclip FLA