Monday, July 14, 2008

Clone functions in ActionScript 3

In AS3 it is a bit of a pain to duplicate an loaded SWF. There are a few ways to do this but they are a bit complicated. So from reading a Design Patterns Book, it gave me the idea of using a clone function on all objects that you are going to load into a flash Object.

The concept is pretty simple once you wrap your head around the concept.

Create a Class

After the constructor make a public function that creates a instance of itself.

Have that function then return that instance.



So it would look like this


Parent Movie:

var instanceMovie:ClonedMovieClass = new ClonedMovieClass();
var clone:ClonedMovieClass = instanceMovie.clone();

ClonedMovieClass:

public function ClonedMovieClass() {}

public function clone():ClonedMovieClass
{
return new ClonedMovieClass();
}


Thus you can have any number of copies of a loaded SWF or object. Now its not perfect and I will be adding more to this in a few days, but the basic concepts work.

No comments: