The Library, Classes and MovieClips in AS3
![]() |
Post Page Rank |
This post describes some of the differences I have stumbled over whilst working with actionscript 3.0 and Flash CS3 compared to earlier versions.
It starts with Export for Actionscript
In AS2 if you wanted to access the properties of an instance on the stage from code, you had to ensure that the “export for actionscript” checkbox was selected in the linkage properties for that MovieClip in the library. If it wasnt your code wouldn’t see the instance and would fail silently. This was pretty confusing at first and even once you got used to it there would be times that it would still catch you out.
When you dont need to Export for ActionScript
In AS3 you do not need to select the “export for actionscript” checkbox to access the properties of an instance on the stage. Any MovieClip instance you drag onto the stage at design time becomes a property of the document class and can be accessed from code as long as it has an instance name. This is only true when the “Automatically declare stage instances” option is checked in Publish Settings - Actionscript 3.0 Settings, which it is by default.
If you do select “export for actionscript” for a clip in the library, like this.

When you click OK you will be greeted with the following slightly alarming pop-up message.
ActionScript Class Warning
A definition for this class could not be found in the classpath, so one will be automatically generated in the SWF file upon export.
This is actually nothing to worry about but may induce mild panic, you just click OK to continue and your swf still works fine. What the message is saying is that you are creating a new type, in other words a new class.
The MovieClip you have created in the library extends the definition of the flash MovieClip and adds extra features to it, at this point just the graphics you have drawn in it. The message is complaining that this extension of MoviClip doesn’t have a definition in code, there is not an .as file for it. However it lets you off lightly and generates the code inside the swf ( no .as files ) at compile time for your new type.
When you do need to Export for ActionScript
In AS3 “export for actionscript” should be selected when you want to add a MovieClip to the stage at runtime from actionscript. The syntax for doing this in AS3 with a MovieClip called Square in the library ( with export for actionscript selected ) looks like this.
mySquare_mc.x= 100;
mySquare_mc.y= 100;
addChild (mySquare_mc );
In the first line an instance of the new type Square is created and named mySquare. At this point mySquare doesnt exist on the display list ( so cant be seen on the stage ) it only exists as a variable in actionscript. The next two lines set some of the properties of the mySquare variable, in this case position. Then finally with the addChild command I hook the mySquare MovieClip variable onto the display list. mySquare now exists as a variable in actionscript and is referenced and displayed on the stage.
In AS2 it was easy to think of MovieClips as things on the stage that your code could reference but with AS3 it becomes clear that a MovieClip is actually a type just as String or Number are.
In the case above Square extends MovieClip. In purely Object Oriented terms this means that if I create an instance of Square it will have all the methods and properties of a MovieClip such as mySquare.x or mySquare.visible plus any extra I add to it. However so far I havent provided code for any extra methods hence the ActionScript Class Warning when the clip is created in the library. If I wanted to add extra methods to Square I could do so by adding a new .as file called Square.as if I wrote the .as before creating the clip in the library I wouldnt get the warning.
Square.as
{
import flash.display.MovieClip;
public class Square extends MovieClip
{
public function sayHello()
{
trace( "Hello" );
}
}
}
Timeline code in my fla file
mySquare_mc.x= 100;
mySquare_mc.y= 100;
mySquare_mc.sayHello();
addChild (mySquare_mc );
The output now traces “hello”. As well as using the standard MovieClip methods and properties mySquare can also call the new method sayHello because it is of type Square and Square extends MovieClip.
One last point, notice that this time I have declared mySquare as type MovieClip and before I declared it as type Square. Either could be used, this duality of type is what is known as polymorphism in Object Oriented speak.
Hope that is of use to somebody!
Tags: actionscript, ActionScript3.0, AS3, export for actionscript, extends, MovieClip, polymorphism

May 27th, 2009 at 4:54 am
When i try this i get the following error:
TypeError: Error #1006: sayHello is not a function.
at squarefla_fla::MainTimeline/frame1()
May 27th, 2009 at 9:41 am
@cookie make sure the Square.as file is in the same folder as your fla. Make sure you have a symbol in the library called Square that you export for actionscript.
Thanks
May 27th, 2009 at 2:04 pm
ok, lol… I figured out the problem.
My square.as file didn’t have the capital S.
May 27th, 2009 at 2:30 pm
The capital letter man-trap strikes again
September 17th, 2009 at 4:50 pm
Nice little explanation. I have always had trouble figuring out how to use the export for as. I wish Adobe would explain things a simple as you do.
September 18th, 2009 at 9:52 am
Thanks very much, MW!
November 3rd, 2009 at 9:49 pm
After adding the mc to the stage, can you access the Square class from the mc timeline?
By that I mean how would you call the sayHello function from the timeline of the mc you just added to the stage?
November 3rd, 2009 at 10:16 pm
nm I just had typo in my code that I overlooked for a very long time
November 5th, 2009 at 4:54 am
Great tutorial, thanks.
Learnt more here than reading 50 pages of an action script eBook.
The beginning concepts are difficult to understand for noobs, but this really helped me know how that scripts are classes and referenced in doc class as objects.
Thanks again, hope you do more
November 5th, 2009 at 2:01 pm
Hey, thanks for your kind words KS. I found writing this helped me to firm everything up in my own mind.
Glad you got it fixed Junie.
February 15th, 2010 at 12:39 pm
Hi,
It was handy. The most imp thing was that I got it xactly when I needed.
Thanks a lot Graeme. And I really mean the “Thanks”