In this Document Class example I introduce several aspects; class coding, packages and some of my development styles. (styles vary from developer to developer) I’ve created these styles for efficient code passing between developers and the styles can also be applied to; Java, Javascript, .NET and AJAX development.
Alright, let’s get started. (You can get the source files here…)
We’ll start by creating a folder in your favorite location. Open this folder and create another folder named “com”. Putting classes in a com folder is pretty standard across Flash developers and will be to your benefit to start using.
Now open Flash and create a blank AS3 document. Name the file DocumentClassExample.fla and save the file to the folder you created in the previous step.
Now create a blank AS3 Actionscript file titled DocumentClassExample.as and save it to your com folder.
Now for the fun part!
In your AS file you’ll need to define the package, class and initiator. The package is what usually trips people up when they start working with AS3. The package can be blank if you have it in the root directory of your FLA, but since we put it into the com folder, we need to define the com package.
package com
{
}
Now that we have the package defined, we need to add the class definition. The class needs to be titled the same as the AS file, DocumentClassExample.as.
package com
{
public class DocumentClassExample
{
}
}
Now this would be fine for a “normal” class file, but the Document class file has to extend MovieClip. The main stage area in AS3 is a MovieClip DisplayObject, so the Document class needs to extend MovieClip. (remember in AS3 you need to import all of your classes!)
package com
{
import flash.display.MovieClip;
public class DocumentClassExample extends MovieClip
{
}
}
Now for the class initiator… The initiator is a function in the class named the exact same as the class. Hopefully you’ve noticed a pattern here; the as file, class and initiator function all need to be named EXACTLY the same. (case sensitive)
package com
{
public class DocumentClassExample extends MovieClip
{
Public function DocumentClassExample() {};
}
}
So we can make sure this class is working when we publish, let’s add a variable that will trace when the FLA is compiled.
package com
{
import flash.display.MovieClip;
public class DocumentClassExample extends MovieClip
{
private static var traceText:String = "Hello World";
function DocumentClassExample() {};
}
}
To call the trace method we’ll add a secondary initiator to the class. This is a pretty typical process for development. We’ll place the trace statement within the init() function.
package com
{
import flash.display.MovieClip;
public class DocumentClassExample extends MovieClip
{
private static var traceText:String = "Hello World";
function DocumentClassExample() { init() };
private function init():void { trace(String(traceText)) };
}
}
We’re almost done, one last thing to do… define the Document class in the FLA. Jump back to your FLA and click on the stage. Open the Properties panel and in the Document class: field enter your newly created Document class. Just like in the AS file we need to make sure we enter the package first [com] and then the class name [DocumentClassExample].
com.DocumentClassExample
Now ctrl+Enter and publish the FLA. You should see “Hello World” trace into the output panel!
You can download the source for this project from the link below. I’ve commented just about all of the code, so happy coding and make sure to let me know what you think and some future topics you’d like covered.
Cheers!