Flash cs5 code location confusion

145 Views Asked by At

Whenever I place the following code on the first frame of my movie, it works fine:

var strGlobal:String = "Global"; 
function scopeTest() 
{ 
    trace(strGlobal); // Global 
} 
scopeTest(); 
trace(strGlobal); // Global

But when I remove it and place it in my document class, it errors out: "Call to a possible undefined method scopeTest" "Access to undefined property strGlobal"

I am new to actionscript 3 and was wondering what to change to make this simple example work from my main document class.

2

There are 2 best solutions below

0
On BEST ANSWER

In the same directory as your .fla file create a file named ScopeTest.as and define the following class:

package  {
    import flash.display.MovieClip;
    public class ScopeTest extends MovieClip  {
        var strGlobal:String = "Global"; 
        public function ScopeTest() {
            trace(strGlobal); // Global 
        }
    }
}

Then inside your .fla project (document class):

enter image description here

Then when you run it you should get "Global" traced out to confirm it works.

0
On

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:

  1. 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).
  2. Your class name.
  3. 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:

  1. 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.
  2. Your property name.
  3. Your property type.
  4. 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:

  1. Namespace - methods use the same namespaces as properties (see above).
  2. Method name.
  3. Method arguments.
  4. Return type.
  5. 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.