How to Create an Array of Movie Clips in ActionScript 3.0
ActionScript 3.0 is still relatively new at the moment, and as a result there appears to be a shortage of information regarding many of it’s features on the Internet. I ran into some problems when attempting to generate new Movie Clip objects on the fly during run-time.
More specifically, I wanted to be able to create an array of stars, which were Movie Clip objects in my library panel. Sure, I suppose I could have manually dragged these stars onto the stage, assigned each of them their own unique instance name, and then used ActionScript to manage them during run-time. The problem in doing it that way is that it is both inefficient and time consuming. Additionally, I would be limited to the amount of stars that I had previously created. What if I wanted another star?
I searched Google for quite some time on this topic before finally giving up and going back to re-working things myself. A rather conservative three hours later, I finally got things to work exactly how I wanted. Here’s how it’s done:
First of all, make sure you’re project is created with ActionScript 3.0; this is important. If you are still programming in AS 2.0, I highly recommend making the shift, as there are a lot of neat things you can do with this new event-driven language.
Once you’ve made your Movie Clip, go to your library panel. By default, the linkage field should be blank. What this means is that there is no class associated with it; objects must be associated with a class in order to be dynamically created through code. That is a general rule for object-oriented programming. In order for you to be able to treat a Movie Clip as an object, you must first create a class for it. Your panel should look something like this:

Notice how the linkage field for the objects are all blank. Right click the Movie Clip of your choice and choose properties. In my case I want to be able to create a class for my star object. In the properties window you’ll notice a check box titled “Export for ActionScript”, which is left blank by default. Check this box, and then give your object a class name. In my case, I kept the class name as the same name as my object.

After you fill out the properties window like in the image above, you will notice that your library panel now has a linkage value next to your Movie Clip. You can now finally create instances of these Movie Clip objects during run-time via ActionScript. Make sure your library panel looks like the image below; if it doesn’t, you must have done something wrong in the process. Try checking your properties again for that Movie Clip and make sure it matches the settings in my example.

Making new objects through ActionScript is simple; here’s a little code snippet on how I created my first dynamic star:
var stars = new Array();
stars[0] = new star();
addChild(stars[0]);
This is obviously not a very practical use of an array, as there is only a single star. You will most likely want to create a loop to initialize several new objects at once, but my goal was to figure out how to create such an array; I will put this knowledge to use when necessary.
One last thing. When you want to remove your Movie Clip from the stage, make sure to call the function removeChild(object_name). If you don’t, you will forever have that Movie Clip on the stage.
I hope that helped.
Related Articles:
- Font Types and File Size in Flash When you are adding new text fields to your Flash project, be aware that they can have a large impact...
- How to Use Layers to Organize a Flash Project Organization is of the utmost importance when creating your Flash project. Keeping your project clean and easy to access will...
- Introducing Planet Yook As I’ve stated in the past, a new Flash-based role-playing game is in the works. The title of this game...
- Working with Adobe Flash CS4 Professional I recently acquired a license for Adobe Flash CS4 Professional Edition. I must say that a lot has changed since...