Saturday, January 13, 2007

DuplicateMovieClip() method in Flash

I've just found out that the duplicateMovieClip() method in Flash can be used to create interesting text effects easily. In this post, I will introduce a simple text effect using this method.

However, firstly, I will show how to change a content of a movie clip from outside. This technique is often used with the duplicateMovieClip() method to create variants of the original movie clips.

Now, I create a movie clip named "clip". In this movie clip, there is only a dynamic text box which is named "char". The content of the dynamic text box doesn't matter (I chose the letter 'A').


Now, we drag the movie clip into the stage and press Ctrl-Enter, it will show the letter 'A'. To change the content of this "clip" movie clip using ActionScript, we only need to type 1 of 2 lines of code into frame 1.

clip.char.text = "B";
or
set("clip.char.text", "B");

I prefer the way using the set() method as it is more flexible. You can do something like this set("clip.char"+".text","B").

So, it's time to use the duplicateMovieClip() method. This method has 3 parameters which are the name of the original movie clip, name of the copy and the depth (a movie clip having a bigger depth will be shown on top of the movie clip having a smaller depth). For example, to show a string "PHONG" (my name) on the screen, we use this code:

str = "PHONG";
size = length(str);
i = 1;

// hide the original movie clip
setProperty("clip", _visible, false);

for (i = 1; i <= 5; i++){
// create a copy of the original movie clip
duplicateMovieClip( "clip", "clip"+i, i );

// set the content of the copy
set("clip"+i+".char.text", substring(str,i,1));

// set the location of the copy on the screen
setProperty("clip"+i, _x, 20*i);
setProperty("clip"+i, _y, 100);
}

This is the source file for this text effect.

No comments: