The Library, Classes and MovieClips in AS3

July 7th, 2008

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.

If you do select “export for actionscript” for a clip in the library, like this.

class Square

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 MovieClip 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.

1
2
3
4
5
var mySquare_mc:Square = new Square();
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

1
2
3
4
5
6
7
8
9
10
11
12
package
{
import flash.display.MovieClip;

public class Square extends MovieClip
{
public function sayHello()
{
trace( "Hello" );
}
}
}

Timeline code in my fla file

1
2
3
4
5
6
7
var mySquare_mc:MovieClip = new Square();
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!

Entry Filed under: flash

5 Comments Add your own

  • 1. Stijn Fastenaekels  |  April 6th, 2009 at 8:41 pm

    Thanks! This helped alot because I have a great book on AS3, but not a single word was spent on explaining how AS3 comminucates with library classes.

    Cheers,
    Stijn

  • 2. Meta  |  April 22nd, 2009 at 1:14 pm

    Best explanation found so far on the net. Thank y ou for making it simple. it is not as simple at it was in as2.

  • 3. Mike  |  June 13th, 2009 at 9:50 am

    Good explanation, but how about a movieclip inside a movieclip that has already been exported for actionscript at runtime? Do I need to export the movieclip inside as wel??

  • 4. graeme  |  June 15th, 2009 at 10:49 am

    Hi Mike,

    I investigated this.

    You do not need to export inner movieclips. It is optional.

    In the Square example above I edited the Square movieclip in the library and dragged a newly created Circle movieclip on to the first layer inside Square. The example still compiled.

    I then gave the circle movieclip a stage instance name of testCircle. This allowed me to reference and alter properties of the testCircle clip from actionscript inside Square.as.

    I then exported the Circle movieclip for export and extended the functionality of it by writing a class for it, similar to Square, and saved it as Circle.as. I created a constructor function for Circle and added it to Circle.as that would be called when any Circle clip gets created.

    public function Circle()
    {
    trace( “Circle constructor” );
    }

    I added a similar constructor to Square. The output shows that the inner testCircle is created before the Square.

    If you are having problems watch out for Capitalization of the class names and their .as file names. They must match.

    Beware that you can alter the default behaviour of the flash compiler for a fla by entering Publish Settings -> Flash and clicking the actionscript 3.0 settings button. There is check box for ‘Automatically declare stage instances’. If you uncheck this you will need to write declerations for each stage instance before you can use them.

    Hope that helps

  • 5. James  |  August 19th, 2009 at 1:10 pm

    Hi

    Can you access a movieclip on stage (which has an instance name) from within a class that isn’t the Document class?

    (In my current project I don’t have a Document class and most of my AS3.0 code is on the timeline. I’m just starting to introduce some external .AS class files to perform various tasks however I can’t seem to get hold of the instances that I know exist on stage).

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed

Calendar

June 2017
M T W T F S S
« Feb    
 1234
567891011
12131415161718
19202122232425
2627282930  

Categories

Most Recent Posts

Feeds