AS3 Basic Setup

July 2nd, 2008

Writing your first Actionscript 3.0 application with Flash CS3? Here is a quick guide to get the basics set up.

The basic set-up includes one .fla file and one .as file.

First create a fla. You can name this whatever you like. Maybe myFirstApp.fla. At this point you could start writing code directly into a frame on the timeline however best coding practice is to define a document class.

The document class can be specified by entering the name of a class into the property panel for the document. This is the same place where you would specify the size of your swf. Think of a name for your document class, maybe: myFirstApp. The document class doesnt have to have the same name as your main fla there is no link between them.

By typing myFirstApp into the property panel you are telling flash to create an instance of the myFirstApp class when the fla runs.

When compiling your fla flash will go and look for the code for the myfirstApp class. It will look for a file called myFirstApp.as by searching the directory the fla is in or in the directories specified under classpath from preferences.

The next step is to create the myFirstApp.as file. This just a text file with the extension changed to .as. Place the file in the same directory as your fla.

Contents of myFirstApp.as

1
2
3
4
5
6
7
8
9
10
11
package
{
import flash.display.Sprite;
public class myFirstAppextends Sprite
{
public function myFirstApp()
{
trace("hi");
}
}
}

The package tag places the myFirstApp class into the root package. Which means this class wont be stored in a subfolder just in the same directory as the fla.

The myFirstApp class extends Sprite. This means that the document class as well as having any further methods and propertys that you define will also have all the methods and properties of a Sprite. The import flash.display.Sprite statement lets the flash compiler know where to look for the definition of Sprite at compile time.

Note that the myFirstApp.as file should only contain a class called myFirstApp or you will get errors.

The document class unlike other classes must extend Sprite because the document class automatically represents the swf stage. Any movieclips instances you name and place on the stage at author time become properties of the document class and can be accessed by name from actionscript inside the class.

This part defines the constructor for the myFirstApp class.

1
2
3
4
public function myFirstApp()
{
trace("hi");
}

The constructor function for a class is a function that has the same name as the class and is called whenever an instance of the class is created. For the document class an instance is created when the swf runs. This means that the constructor is the entry point for our application and the trace statement is the first line of code to be executed at runtime. The creators of flash have borrowed these concepts from the java world.

And thats the basic set up!

Entry Filed under: flash

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

May 2017
M T W T F S S
« Feb    
1234567
891011121314
15161718192021
22232425262728
293031  

Categories

Most Recent Posts

Feeds