Working
with attachMovie and duplicateMovieClip
actions
Introduction
This brief tutorial illustrates how to work with the attachMovie and duplicateMovieClip
actions in Flash. These two actions are quite similar. They both allow
you to take a movieclip and duplicate it however many times you want.
You must give these duplicates instance names and then target them in
any way you would target a movieclip that you manually placed on the
stage.
Open Flash and change the stage size
of the new document to 300X300 and the
frame rate to 21fps.
Draw a small (button sized) rectangle on the stage. Type "reset" (using
static text) on top of your rectangle. Select both the text and the rectangle
(at once) and convert them to a movieclip - name it "resetButton" and
ensure that the registration point is set to the top-left corner.
Now delete the resetButton mc from the
stage so that it only exists in the library.
Select it in the library and from the drop
down menu up the top-right of the library
choose "linkage". Check the "export
for actionscript" box and give it
an identifier of "myResetButton".
This is vital. If you do not
tell Flash to export your symbol like this
then, when it comes to publishing your movie,
Flash will look at the stage and go "hmmm
well that symbol didn't end up being used
in this movie so I won't bother putting it
in the swf". Setting up the linkage
like this simply forces Flash to export our
symbol so we can call it via actionscript.
What does this do? Well stop() stops the main timeline. The next line
attaches our reset button to the main timeline (_root). It does this
by first looking for the symbol with the identifier "myResetBut".
When it finds it, an instance of our mc is attached to _root and given
an instance name of "newResetButton". It is attached at a
depth of 200.
Test your movie.
You will notice that Flash aligns the
registration point of the mc we are attaching
with the registration point of whatever
we are attaching it to. In this case,
we are attaching it to _root so the registration
point if the top left corner of the stage.
Maybe we do not want it there. Our new
instance has an instance name so we can
position it where ever we want. Add this
code under the other actions:
newResetButton._x=200;
newResetButton._y=250;
What this does is move our instance
to an x position of 200 and a y position
of 250. Note - Flash will align the registration
point of our mc (which is the top left
corner in this case) with the co-ordinates
that you give it.
Our button is positioned where we want
it on the stage but its not doing anything.
Click on the drop down menu in the library
and select "new symbol". Make
it a movieclip, name it "shape1" and
click OK. You should automatically be inside
your new mc so draw a shape. Mine looked
like this but do whatever you want:
Click on shape1 in the library
and open up the linkage window. Check the "export
for actionscript" box and leave its
identifier as "shape1". Make another
new symbol from the library drop down menu.
Make it a movieclip, name it shape2 and again
draw whatever you want inside it. Set up
the linkage for the shape2 mc and leave its
identifier as "shape2".
Click on the "scene 1" button up the top left to get back to
the main timeline. You should have nothing on your stage and 3 movieclips
in your library (with linkage set up for each of them). Ok now we need
to get our shapes on to the stage. Lets use attachMovie again but this
time make our button do the attaching. Add this action under the others
in frame1.
The shapes are attached to the main timeline and again have
their registration points aligned to the top-left corner of
the stage. Shape2 appears in front of shape1 because it was
attached to a higher depth (6). OK now I'm sure you're all
totally blown away by what you have seen so far but don't pass
out from excitement just yet.
Let's use a couple of for
loops to make our button attach many copies
of our shape movieclips. Change the actions
that you already have for the button to:
What this does is make 20 copies of shape1 and 10 copies of
shape2. After making each copies, Flash places the new instance at a
random position on the stage, rotates it a random amount and gives it
a random alpha value. You will notice that all the shape2 copies are
still appearing above the shape1 copies. This is because they are placed
on a depth between 50 and 59, whereas the shape1 copies are placed on
a depth of between 0 and 19 (depending on what i was up to at the time
- by this I mean i the variable, not I as in me :) ).
Test your movie again and click on the
button a few times. Everytime the button
is click, new instances of our mcs are
attached which replace the old ones (because
they are being attached into the same
depths). You can get some nice effects
with this.
You can download this attachMovie example
from here.
Let's look at duplicateMovieClip.
The only real difference between attachMovie
and duplicateMovieClip is that attachMovie
takes your mc our out of the library via
its linkage name and makes a copy of it,
whereas duplicateMovieClip needs the mc you
want to copy to be on the stage with an instance
name. Lets make an example of duplicateMovieClip.
Make a new document in Flash - the default
size and everything will do.
Draw a rectangle down the bottom of
the stage somewhere and convert it to
a button - name it myButton and give
it an instance name of "button1".
OK now draw a circle and convert it to
a movieclip. Name it myCircle and give
it an instance name of "circle1".
Move circle1 off to the left of the stage.
Make a new actions layer and place this
action on the first frame.
The first line tells us that this is
an action that will happen when we click
on button1. We then get into another
for loop. This time I have used "hiThere" as
our variable just to prove to you that
it really doesn't matter what you name
your varaibles ("i" is not
a magical letter). So you can see from
the parameters of out for loop that the
code underneath with run 7 times. Each
time it is run, our circle1 mc is duplicated,
given a new (and unique) instance name
and placed on a depth between 1 and 7.
We then position our duplicate at 100
y. To get the new x position, we multiple
hiThere by the width of circle, which
will make all our copies line up right
next to each other.
Test your movie and click on the button.
Have a play with attachMovie and duplicateMovieClip
and try and do something interesting
with it.
Wildform
provides a 100% satisfaction guarantee on all our software.
If you are not completely satisfied with our software
for any reason you may request a refund within 15 days
of purchase.