Each developer has a favourite IDE that he or she swears by. I’m an Eclipse man myself. However, during a project I worked on recently, I had the misfortune of diving back into development using Flash CS3, a tool that, despite it’s widespread use, I have managed to mostly stay away from since it’s release. Why? Flash CS3 is a BAD tool for ActionScript development*. Needless to say I was a bit rusty with the Flash authoring tool, but I was nonetheless determined to create a nice, clean, object-oriented, design-patterny product.
*Perhaps I shall elaborate on this statement in a later post
The first thing I did was create some MovieClip symbols. I created, for example, a custom button containing a background (to which i gave the instance name “bg”) and a text field (called “label”). I then opened the properties for this button and included a path to a class (com.foo.MyButton) that would be associated with the symbol. In the class, I declared the two child MovieClips as variables like so.
package com.foo {
import flash.display.*;
import flash.text.*;
class MyButton extends MovieClip {
public var bg:Sprite;
public var label:TextField;
}
}
However, when I ran this code it seemed to choke on the two variable declarations.
I was stumped. Was Flash CS3 actually unable to deal with this situation? Many people I asked seemed to think so. Eventually, I talked to someone who knew the solution.
Sarah Plowright of ActionScriptGirl.com had this advice:
Remember how if you place a MovieClip on the stage or in another MovieClip in an AS2 project you had to also declare a variable of the same name if you’re using a custom class? Well, in Flash CS3 / AS3, Adobe has decided to give us developers some options. The only problem is, they didn’t exactly make these options obvious to the user. If you didn’t know about this change, you be getting all sorts of nasty errors and wondering why you ever decided to build that project using Flash instead of Flex.
If you’re getting errors along the lines of
"1151: A conflict exists with definition myMC in namespace internal."and you’re working custom classes attached to library objects with instances, the error is probably caused by the fact that you have “Automatically declare stage instances” checked. This checkbox is located under Publish Settings > Flash > ActionScript 3.0 Settings. If it’s checked, Flash will automatically add variables to your class at compile time. If you already have tried to declare a variable in your class for this instance, the compiler will throw error 1151, as you will now have two declarations of the same variable. If it’s unchecked, you will have to declare a public variable for every onstage instance that you want access to in ActionScript. Remember, you must make your instance declarations public, or they won’t work!The fix is easy, simply decide whether or not you want Flash to create your variable names for you. I personally prefer to declare my own stage instances as I believe it’s a better practice to have all your variables listed at the top of a class, but annoyingly, this means I will have to uncheck the “Automatically declare stage instances” checkbox for every new FLA file. Unfortunately, this option only seems to be available under publish settings, and not available under the global application preferences.
Here are some sample files that show’s how this all works.
I happen to agree with Sarah that the best way to work is to uncheck the checkbox and declare all of your variables yourself. This is the only way that really enforces good practices when you are using classes behind your MovieClips in the library.
Thanks for the help, Sarah!


We currently have 2 ways of using Flash elements in our AS projects:
1. Embed the part
This only works if the part does not contain any components. When embedding the AS is removed, so simple stop actions are automatically done with frame labels.
2. Export to SWC
We give the MovieClip a specific class, set the ‘export swc’ check and include the swc in the AS Project. We then can simply extend the MovieClip and add functionality.
Greetz Erik
this was always driving me nuts(for more than 2 years)…i used to use public static vars only for name instances. good read..thanks
Great Post, Thank you, but I have a question, what if I have to have many hand-positioned mcs on stage and they all have to be populated in a specific order, do I have to declare each and every one of them individually in the class or is there a way to loop through them and populate them based on their instance names?
@Leo
I’m not sure I understand the question exactly. If you have elements positioned on your stage manually, each one that you need to have specific access to needs an instance name. I suppose that you could work around this by walking through the children in the display list too. If you needed to execute something in a particular order, you could always just place references to all your child MCs into an array and step through them, but you’d still need to give each one an instance name. e.g.
public var foo:MovieClip, bar:MovieClip, baz:MovieClip;
public var allMovieClips:Array;
// in your constructor…
allMovieClips = [foo, bar, baz];
Does that answer your question?
@Leo
what if I want to display several movie clips (taken from my library) on stage with for loop and assign each of them a unique name like mc1, mc2, mc3, etc.? How can I do that in AS3 and how can I access them for instance to change their position via for loop again?
Have you found anyway to get this to work with classes that have optional parameters? I have a class that works perfectly in every way if i use addChild, but if i try to use this method for a stage instance i cannot override the default parameters of my class.
Pingback: Should I check it or not! Uncheck it, and leave a note « Ramblings
Hey man,\n\nCS3 really isn’t a bad tool for dev. I use it everyday and can do a lot of code rather fast.