Flash Script – Create Duplicate Movieclips


type = 'application/x-shockwave-flash'
data = 'http://sitepointstatic.com/flash/duplicatemovie.swf'
width = '400'
height = '400'
bgcolor = 'black'
>

Download the sample files here.

Note that this tutorial assumes that you know how to create buttons and text fields.

1. First, create a button and label it "make clones".

2. Create a text field as shown in the above movie. Give it a variable name "no_clones".

3. Create a movie clip of your choice, which we can duplicate. I created a circle object with a gradient.

4. Give the movie clip an instance name of "clone".

5. Now, in the default layer’s first key frame, insert this action:

// set variable clips=value in text field. 
clips = no_clones;

6. Right click the button and insert these actions:

/on (release)  
{
// get the current x pos of the clone movie
mov_x = getProperty("/clone",_x);
//Check if the text field is empty if empty set it to 1
if(no_clones == "")
{
no_clones = 1;
}
// Use for loop to remove existing movie clip
for(i=1; i<=clips; i=i+1)
{
removeMovieClip ("/clone" add i);
}
// Use for loop to create duplicate movies.
for (i=1; i<=no_clones; i=i+1)
{
mov_x = mov_x + 50;
duplicateMovieClip ("/clone", "clone" add i, i);
setProperty ("/clone" add i, _x, mov_x);
}
//set variable clips = no_clones.
clips = no_clones;
}

I’ve commented the above script so that it’s easier for you understand. But that’s all you need to do: it’s time to test the movie.

Learning to create duplicate movie clips in flash is very usefull. It can be used to create a lot of effects — happy experimenting!

Win an Annual Membership to Learnable,

SitePoint's Learning Platform

No Reader comments

Comments on this post are closed.