Note that there are some explanatory texts on larger screens.

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

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload