Groups | Blog | Home
all groups > flash actionscript > april 2005 >

flash actionscript : Using duplicateMovieClip();


fasterthanlight
4/7/2005 12:00:00 AM
I must be missing something! No matter what I try.... all I get is blank. I've
included the .fla in this message, and if you could just give it a quick look
see I would be much appreciative. Thanks again.


www.jamieroy.com/testWindows.fla.zip
fasterthanlight
4/7/2005 8:03:06 PM
Hey there, I'm using the duplicateMovieClip(); command to create a whole bunch
(depending on a variable, in this case its called sum) of empty movieclips that
will load pictures into them depending on which number they are.

Here is the code I've come up with, and as usual, it doesn't work:

var sum:Number = 0;
for (var i = 1; i<=10; i+=1) {
sum += 1;
duplicateMovieClip(_root.dup, dup_mc[sum], this.getNextHighestDepth());
dup_mc[sum]._x = _root.dup_mc[sum-1]._x+dup_mc[sum]._width;
}

So basically, "dup" is the clip that is getting duplicated, and dup_mc[sum] is
the clip that is created, and it's name is dependant on sum. This code SHOULD
create about 10 movieclips, and place them next to the one that was created
just prior to them. Any help is greatly appreciated.
SymTsb
4/7/2005 8:13:20 PM
I just finished writing code that does just this very thing. Here you go...
The code will actually create 4 rows by 8 columns. A little more than what you
need but it is exactly what you asked for.

function createWindows(){

// Grab the initial _y coordinate of our main window from the stage
yPos = this.window._y;

// We want 4 rows
for(r=0; r<4; r++){

// We want 10 columns
for(c=1; c<=8; c++){

// Which window are we going to create on this run of the columns loop?
clipCounter = (r*8)+c;

// Duplicate our base window clip and set the _alpha (transparency) to 0
(hidden)
duplicateMovieClip("window", "window"+clipCounter,
this.getNextHighestDepth());
this["window"+clipCounter]._alpha = 0;

// What is our x-coordinate going to be? If we are in the first column, it
will be the _x of
// the original clip. Otherwise, it will be the _x or the original clip +
an offset for each
// column after 1.
if(clipCounter == (r*8)+1){
this["window"+clipCounter]._x = xPos;
} else {
this["window"+clipCounter]._x = this["window"+(clipCounter-1)]._x +
this["window"+clipCounter]._width + 15;
}

// Set the _y value for our new window clip
this["window"+clipCounter]._y = yPos;

// Load a jpeg into the new clip using our Movie Clip Prototype functions
//
this["window"+clipCounter].preload("gallery/thumbs/Image"+clipCounter+".jpg");

// Increment the total number of kilobytes that have to load now that this
movie is loading.
// kBytesTotal += this["window"+clipCounter].kbTotal;
}

// Increment the _y for the next row and reset _x to the original clps _x
yPos = yPos + this.window._height + 15;
xPos = this.window._x;
}

// Make sure that the master movie clip we duplicated is _alpha'ed out of the
scene.
this.window._alpha = 0;
}
fasterthanlight
4/7/2005 10:24:04 PM
Hey man, thank you SO much for responding. The only problem is, I have no idea
how to implement it. Could you please give me some details on how you have your
movie set up? IE: Where in the movie is the mc that you are duplicating so that
I can change it to work with mine...
SymTsb
4/7/2005 10:39:56 PM
Yeah not a problem.

Movie structure is pretty simple. 1 Frame with 2 layers. top layer is my
script and layer 2 contains the movie clip named window. In my case, the
window mc is 35x35px although that is irrelevant to the script. What is
relevant, is where the window mc is placed on the stage as it serves as the
initial x and y coordinates for the entire script. Then all you do is place my
script in frame one and then call createWindows().

For the sake of future additions of movie clips or say you want to eventually
add multiple rows, I would leave the script intact. But for your current
purposes change the following lines of code..
==============================
// We want 4 rows
for(r=0; r<4; r++){

change it to...
for(r=0; r<1; r++){
==============================
// We want 10 columns
for(c=1; c<=8; c++){

change it to...
for(c=1; c<=10; c++){
==============================
// Which window are we going to create on this run of the columns loop?
clipCounter = (r*8)+c;

change it to...
clipCounter = (r*10)+c;
==============================
if(clipCounter == (r*8)+1){
this["window"+clipCounter]._x = xPos;

change it to...
if(clipCounter == (r*10)+1){
==============================

A few other notes about my script. In places like this...
yPos = yPos + this.window._height + 15;
15 is the gap spacing you want between the clips and can be changed to suit
your needs. Eventually, I will make the script more scalable by changing the
counters and offsets etc into variables that I only have to change in one place
but for now, this works to suit my needs on the current project.
SymTsb
4/8/2005 12:00:00 AM
You are getting blank because my action sets the alpha on the clips to 0. In
my case, I wanted them hidden because the movie clip I created was a box shape
and I didn't want that to show to the audience. find the section of code where
the alpha is set to 0 (should be in 2 places) and set that to 100 or comment it
out.
fasterthanlight
4/9/2005 10:06:03 PM
AddThis Social Bookmark Button