ActionScript, Document Class and Flash Movie

ActionScript, Document Class and Flash Movie
In the last tutorial, we discussed two ways to include ActionScript in your FlashR movie. These methods had the drawback that at least some ActionScript was added to the Timeline. In an effort to move closer toward the OOP programming convention of modular code in an external file, we will now take a look at a third way to include ActionScript into your Flash movie using the Document class.

As a quick review, in the previous tutorial, we were using ActionScript to draw a red circle on the stage. We had placed this code in an external file and added an include statement on the Timeline to tell Flash to call in the external code.

With this next method of associating our ActionScript with our Flash movie, we will remove all code from the Timeline and use the Properties Inspector to declare a Document class which will connect our Flash movie to the external code. (A Document class is a top level class or top level object that is created when your Flash movie is loaded into the Flash Player.)

Our first step is to remove the include statement from the Timeline.

  1. Open your RedCircle.fla file into Flash.

  2. Select the first frame on the Actions layer and open the Actions panel. Remove the include statement and close the Actions panel.

    Now we will declare our Document class from the Properties Inspector. This is our new way of associating our ActionScript with our Flash movie.

  3. With the Selection tool, click on the stage to display the Document properties in the Properties Inspector. At the bottom of the panel, you will see a text input box where you can enter the name of your new Document class. In this dialog box, enter the name of our external ActionScript file without the .as extension. If you remember, we called this file in the previous tutorial. So you will enter "DrawCircle" in the box. Flash may notify you that it will automatically create this new Document class for you. Just click OK.

    Note – It is customary for the first letter in the name of a Document class to be an upper case letter.

    This declaration of a Document class is all we need to do to associate our external ActionScript file with our Flash movie. Now we need to work on that external file. We need to add a few more lines of ActionScript code to the existing code that draws the red circle on the stage. Open the file into Flash and replace the code that is there with the following code.

    import flash.display.Sprite;
    import flash.display.Shape;
    public class DrawCircle extends Sprite
    public function DrawCircle()
    var myCircle:Shape = new Shape();, 0x000000);;, 200, 50);;

    This new code first imports the Sprite class and then the Shape class which we need to draw our circle on the stage. We did not need to do this when our code was on the Timeline. But when our code is external to the Flash movie, we must first import the Shape class before we can use its predefined Graphics property and methods to draw on the stage. Test your movie and you will still get the red circle drawn on the stage.

Copyright 2018 Adobe Systems Incorporated. All rights reserved. Adobe product screen shot(s) reprinted with permission from Adobe Systems Incorporated. Adobe, Photoshop, Photoshop Album, Photoshop Elements, Illustrator, InDesign, GoLive, Acrobat, Cue, Premiere Pro, Premiere Elements, Bridge, After Effects, InCopy, Dreamweaver, Flash, ActionScript, Fireworks, Contribute, Captivate, Flash Catalyst and Flash Paper is/are either [a] registered trademark[s] or a trademark[s] of Adobe Systems Incorporated in the United States and/or other countries.

Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map

Content copyright © 2019 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact Diane Cipollo for details.