AS3 and Flash Document Class

AS3 and Flash Document Class

I’ve been thinking about porting some old ActionScript 2 code to ActionScript 3 and building an AIR app. The big advantage of ActionScript 3 is that it’s faster AND has a new package hierarchy that makes way more sense than the old one. For example, you can now instantiate a MovieClip or TextField with var mc:MovieClip = new MovieClip(), which looks totally normal. In ActionScript 2, you’d actually have to call DuplicateMovieClip() on an existing MovieClip instance.

Anyway, there’s always a little bit of magic involved in setting up a new “Hello World” style program. As is the norm, the official “Getting Started” material that I could find is simplistic to the point of uselessness…who writes these things? I don’t want to print “hello world”; I want to set up the basis of a rich flourishing application. Here’s the basic steps I followed with Flash Professional CS 5.5.

1. Create a New Project

Switched to the “Developer” Workspace Layout, which exposes the Project Panel. Created a new project, which creates a Flash .FLA stub file. We’ll be setting properties here later.

On a side note, the Project Manager is as terrible as I remember it from 2007, but at least it is just a mirror of an underlying directory structure. Nothing fancy here!

2. Create the Startup Class

The Flash file will end up holding any static library assets I create such as animations and graphics, but I will be using code attached to instances I create dynamically with my own scripting. Rather than stuff this all in Frame 1 of the timeline, there’s a way of binding a particular class to the Flash file. This is a class that is instantiated on startup. It’s known officially as the document class, and you set it under Properties for the .FLA file.

Class files in Actionscript 3 have the .as extension, and the filename must match the class name. There is also the notion of packages. I created a package path of dseah, which is implemented as a folder relative to the .FLA file.

project_folder/
    dseah/
        MainClass.as
    DaveTest01.fla
    DaveTest01.swf

My DaveTest01.fla file is at the same level as the dseah folder. Inside this folder I created MainClass.as, which is declared as follows:

package dseah { 
    
    import flash.display.MovieClip; 

    public class MainClass extends MovieClip { 
        public function MainClass() { 
            trace ('Hello World'); 
        }
    } 
}

Two Important Notes:

  • See how MainClass extends MovieClip? That’s because the class we’re setting as the document class is a representation of the .FLA file, which is a MovieClip.
  • When we go to properties to set the document class, we also have to use the entire package path, which here is dseah.MainClass.

3.0 Compile and Test!

I clicked the “Test Project” button and did see that the trace worked, outputing “Hello World” to the console. Whee! So that’s it. Took me 90 minutes to dig up these basic setup facts while I was at Starbucks. I suppose that if I’d had a copy of Colin Moock’s Actionscript 3: The Complete Reference I would have saved myself some time. Grumble grumble.

0 Comments