Deprecated: Assigning the return value of new by reference is deprecated in /home/datafake/public_html/blog/wp-settings.php on line 468

Deprecated: Assigning the return value of new by reference is deprecated in /home/datafake/public_html/blog/wp-settings.php on line 483

Deprecated: Assigning the return value of new by reference is deprecated in /home/datafake/public_html/blog/wp-settings.php on line 490

Deprecated: Assigning the return value of new by reference is deprecated in /home/datafake/public_html/blog/wp-settings.php on line 526

Strict Standards: Declaration of Walker_Page::start_lvl() should be compatible with Walker::start_lvl(&$output) in /home/datafake/public_html/blog/wp-includes/classes.php on line 594

Strict Standards: Declaration of Walker_Page::end_lvl() should be compatible with Walker::end_lvl(&$output) in /home/datafake/public_html/blog/wp-includes/classes.php on line 594

Strict Standards: Declaration of Walker_Page::start_el() should be compatible with Walker::start_el(&$output) in /home/datafake/public_html/blog/wp-includes/classes.php on line 594

Strict Standards: Declaration of Walker_Page::end_el() should be compatible with Walker::end_el(&$output) in /home/datafake/public_html/blog/wp-includes/classes.php on line 594

Strict Standards: Declaration of Walker_PageDropdown::start_el() should be compatible with Walker::start_el(&$output) in /home/datafake/public_html/blog/wp-includes/classes.php on line 611

Strict Standards: Declaration of Walker_Category::start_lvl() should be compatible with Walker::start_lvl(&$output) in /home/datafake/public_html/blog/wp-includes/classes.php on line 705

Strict Standards: Declaration of Walker_Category::end_lvl() should be compatible with Walker::end_lvl(&$output) in /home/datafake/public_html/blog/wp-includes/classes.php on line 705

Strict Standards: Declaration of Walker_Category::start_el() should be compatible with Walker::start_el(&$output) in /home/datafake/public_html/blog/wp-includes/classes.php on line 705

Strict Standards: Declaration of Walker_Category::end_el() should be compatible with Walker::end_el(&$output) in /home/datafake/public_html/blog/wp-includes/classes.php on line 705

Strict Standards: Declaration of Walker_CategoryDropdown::start_el() should be compatible with Walker::start_el(&$output) in /home/datafake/public_html/blog/wp-includes/classes.php on line 728

Strict Standards: Redefining already defined constructor for class wpdb in /home/datafake/public_html/blog/wp-includes/wp-db.php on line 306

Deprecated: Assigning the return value of new by reference is deprecated in /home/datafake/public_html/blog/wp-includes/cache.php on line 103

Strict Standards: Redefining already defined constructor for class WP_Object_Cache in /home/datafake/public_html/blog/wp-includes/cache.php on line 425

Deprecated: Assigning the return value of new by reference is deprecated in /home/datafake/public_html/blog/wp-includes/query.php on line 21

Deprecated: Assigning the return value of new by reference is deprecated in /home/datafake/public_html/blog/wp-includes/theme.php on line 618

Strict Standards: Redefining already defined constructor for class WP_Dependencies in /home/datafake/public_html/blog/wp-includes/class.wp-dependencies.php on line 15

Warning: Illegal string offset 'trackoutbound' in /home/datafake/public_html/blog/wp-content/plugins/google-analytics-for-wordpress/googleanalytics.php on line 489

Warning: Illegal string offset 'trackadsense' in /home/datafake/public_html/blog/wp-content/plugins/google-analytics-for-wordpress/googleanalytics.php on line 501

Strict Standards: call_user_func_array() expects parameter 1 to be a valid callback, non-static method GoogleSitemapGeneratorLoader::Enable() should not be called statically in /home/datafake/public_html/blog/wp-includes/plugin.php on line 311
The Library, Classes and MovieClips in AS3

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.

Linkage properties, class Square, base class flash.display.MovieClip

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.

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

package
{
import flash.display.MovieClip;

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

Timeline code in my fla file

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!

Tags: , , , , , ,

11 Responses to “The Library, Classes and MovieClips in AS3”

  1. cookie Says:

    When i try this i get the following error:

    TypeError: Error #1006: sayHello is not a function.
    at squarefla_fla::MainTimeline/frame1()

  2. admin Says:

    @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

  3. cookie Says:

    ok, lol… I figured out the problem.

    My square.as file didn’t have the capital S.

  4. admin Says:

    The capital letter man-trap strikes again

  5. MW Says:

    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.

  6. admin Says:

    Thanks very much, MW!

  7. Junie Says:

    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?

  8. Junie Says:

    nm I just had typo in my code that I overlooked for a very long time

  9. KS Says:

    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 :)

  10. admin Says:

    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.

  11. Muhammad Kirmani Says:

    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” :)

Leave a Reply