duplicateMovieClip() function in Flash Action Script 3

All of us know that duplicateMovieClip() function doesn’t work in Adobe Flash Action Script 3 (AS3) which was very handy function to duplicate the movie clip in previous versions of Action Scripts such as AS1 and AS2. Flash coders who are migrating from AS2 to AS3 are finding it very difficult to duplicate the movie clip and coding the lengthy lines to do the same.  Below is the process on how to shorten your work and duplicate the movie clip with little code.

  1. Open Adobe Flash software, click on “Action Script 3.0” in create new section
  2. Create a rectangle using “Rectangle Tool”, select it and go to “Convert to symbol” (“F8” key) option under modify menu
  3. Let the name as “Symbol 1” itself, click on “Export for ActionScript” check box and write the Class as “block” which was “Symbol1” before. Click “OK”, again click “OK” for “Action Script Class” warning dialog box
  4. Change the instance name of rectangle as “box0” (0 is number)
  5. Place the following code in frame1

import flash.display.*;
var counter=1;
box0.addEventListener(MouseEvent.CLICK,makeblock);

function makeblock(e:Event){

this[“box“+(counter)]=new block;
this[“box“+(counter)].x=this[“box“+(counter-1)].x;
this[“box“+(counter)].y=this[“box“+(counter-1)].y+this[“box”+(counter-1)].height;
addChild(this[“box”+(counter)]);
counter++;

}

  1. Test your movie using Ctrl+Enter and click on the rectangle you have created, you can see a new rectangle below.

That’s it. Here we exported the movie clip to action script as a class named “block” and used “addChild” function to add the another instance of movie clip to the stage. We also specified the new instance name to “box” using the “counter” variable.

Here you can get the attached .fla file of this coding.