Duplicate MC in AS3

So, the other day I was looking for a way to duplicate a movie clip that the user clicks on. In my case, I had lots of buttons made of the MCs that I wanted to copy. I could have made a big switch statement like this:


1
2
3
4
5
6
7
8
9
switch(buttonClicked){
	case buttonA:
		myMC = new MovieclipA();
		break;
	case buttonB:
		myMC = new MovieclipB();
		break;
	//... etc...
}

That just didn’t seem efficient enough for me. So I did some research to find out what happened to the duplicateMovieClip method we had in AS2. Well, I don’t know what happened to it, but here’s the way to do it in AS3:

1
2
3
4
5
6
7
//get the name of the clicked button's class
var buttonClass:String = getQualifiedClassName(buttonClicked);
//get the class definition by name
var ClassDefinition:Class = Class(getDefinitionByName(buttonClass));
//create a new movieclip with this class
var myMC:MovieClip = new ClassDefinition;
//place it on stage or whatever.

Here it is simplified:

1
2
3
var ClassDefinition:Class =
	Class(getDefinitionByName(getQualifiedClassName(clickedButton)));
var myMC:MovieClip = new ClassDefinition;

This doesn’t actually duplicate the movieclip because you’ll still have to copy the properties of the first one to the new one (x, y, alpha, etc). That’s pretty easy to do though, and you might want the default properties anyway.

Here’s the source file for the example above: duplicateMC.fla

Tags: , , ,

One Response to “Duplicate MC in AS3”

 
  1. Robin says:

    This is a really tough game. How do I score points?

 

Leave a Reply