Let's have a look at writing a document class.
Step 1: Declaring the package - package represents where relative to the .fla your document class is stored. If your document class is in the same directory as the .fla, you'll just need to write:
package
If it's in a folder, you'll need to add that after package
. Let's say your document class was in a directory called src
:
package src
Now that that's sorted, you should have something like this:
package
{
//
}
The next thing you need to do is import the classes that you'll need to use in your document class. You'll want to extend MovieClip
in your document class, so lets import that:
package
{
import flash.display.MovieClip;
}
That's all we'll need to cover your example, so now we move onto the third element required, the class declaration. In this case, it is made up of three parts:
- The class namespace - can be either
internal
(if you only want your class to be accessible from classes in the same package) or public
(accessible anywhere in the project).
- Your class name.
- What your class will extend - in this case, MovieClip.
All together, yours will look like this:
public class Document extends MovieClip
Now you'll have something like the below, which means you can start adding properties
and methods
:
package
{
import flash.display.MovieClip;
public class Document extends MovieClip
{
//
}
}
The first thing you'll want to do is create a constructor
for your class. The constructor is called when an instance of this class is created, or in your case being a document class, immediately.
Constructors are defined by creating a method with the same name as its containing class. Constructors must also be public
and return nothing. Here's your new code with an empty constructor:
package
{
import flash.display.MovieClip;
public class Document extends MovieClip
{
// Constructor
public function Document()
{
//
}
}
}
Next step is to create your properties that will belong to your class. In your example you used strGlobal:String
, so let's add that. Properties generally belong immediately below the class declaration and above the constructor. Properties are made up of four parts:
- Namespace - this determines the accessibility of your property. If you omit this, the default will be
internal
. For now, these are the basics:
public
- Your property is accessible from anywhere that have reference to an instance of your class.
private
- Your property is only accessible from within the class - this seems useless at first, but eventually you'll find yourself primarily using this.
protected
- Your property is accessible in classes that extend your class.
internal
- Your property is accessible from classes that are in the same package.
- Your property name.
- Your property type.
- Your property value.
In your case, strGlobal
will look something like this:
public var strGlobal:String = "Global";
Let's add that to your document class:
package
{
import flash.display.MovieClip;
public class Document extends MovieClip
{
// Properties
public var strGlobal:String = "Global";
// Constructor
public function Document()
{
//
}
}
}
Next up, you'll want to create your method scopeTest()
as per your question. Methods are made up of 5 parts:
- Namespace - methods use the same namespaces as properties (see above).
- Method name.
- Method arguments.
- Return type.
- Content.
scopeTest()
doesn't have any arguments and doesn't return anything, so it's going to look like this:
public function scopeTest():void
{
trace(strGlobal);
}
Methods generally belong anywhere below your constructor, so let's slot it in now:
package
{
import flash.display.MovieClip;
public class Document extends MovieClip
{
// Properties
public var strGlobal:String = "Global";
// Constructor
public function Document()
{
//
}
// Output the value of strGlobal
public function scopeTest():void
{
trace(strGlobal);
}
}
}
Now that everything is ready, you'll be able to call scopeTest()
from within your constructor. Because the constructor is called immediately, you should see Global
printed in your output panel (assuming you've linked to it within the Flash IDE correctly).
Hope this helps.