Flash Notes: Setting Up An External ActionScript File
Continuing on from the simple button example you can set up an external ActionScript file that contains all your code.
This is setting up a “document class”. You first create an ActionScript file and save it in the same directory as your Flash file; it will have the extension .as. Then go into your Flash file and in the Properties Inspector in the field “Document class”, enter the name of your ActionScript file. Don’t include the extension “.as”. If you did this correctly, when you click on the pencil icon, your external ActionScript file will open.

Note: If you have your AS file in subfolders, you can point to it using dot syntax relative to your .fla file. You can also set up Flash to look into particular folders as a system preference.
The external code contains the same code you’d have in the .fla file but enclosed in a “package”. Here’s the previous simple button example in a package:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.text.TextField;
public class button extends MovieClip{
public function button() {
myButton.buttonMode = true;
myButton.mouseChildren = false;
myButton.addEventListener(MouseEvent.MOUSE_UP, myButtonUp);
myButton.addEventListener(MouseEvent.MOUSE_DOWN, myButtonDown);
myButton.addEventListener(MouseEvent.MOUSE_OVER, myButtonOver);
myButton.addEventListener(MouseEvent.MOUSE_OUT, myButtonOut);
function myButtonUp(event:MouseEvent):void{
events_txt.text = "Event: MOUSE_UP";
}
function myButtonDown(event:MouseEvent):void{
events_txt.text = "Event: MOUSE_DOWN";
}
function myButtonOver(event:MouseEvent):void{
events_txt.text = "Event: MOUSE_OVER";
}
function myButtonOut(event:MouseEvent):void{
events_txt.text = "Event: MOUSE_OUT";
}
}
}
}
The meat of the code is within the “button” function and is exactly the same as my previous post. It has to be the same name as the .as file. The “package” contains import statements that import various Flash libraries needed by the code. In this case, I had to import the libraries for MovieClip (my button is a movie clip), MouseEvent, (because I’m adding listeners for mouse events) and TextField (because I have a text field on the Stage). I really don’t know exactly what needs to be included for what purposes; I’m learning as I go.
Where this gets very powerful is setting up “class files” for library symbols, allowing them to be more portable across documents.
For more on this subject, check out this QuickStart at Adobe regarding external AS files.
Previous Post:
Add New Comment
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Add New Comment
Trackbacks
(Trackback URL)
May 20, 2008 at 6:59 am
[...] Similarly to a document class, the import statements handle the elements of the button symbol in the library (movie ...