Skip to main content
Inspiring
September 2, 2009
Question

I don't understand packages!

  • September 2, 2009
  • 2 replies
  • 1895 views

I've got this far and it all works: main.fla is in the top level folder and it's document class, Main.as, is in a subfolder named aardvark. Main.as defines itself as being in the aardvark package as do other classes in the aardvark folder, specific to this project. These classes all run and I don't need an import statement to use them. So far so good.

Now I want to create some utility classes which can be used in this or any other project. I put them in C:\as3classes\utils and add the utils folder to the classpath. By using import utils.*, these classes are all available and my objective is achieved but not as I intended. What I intended was to define the classes in a package called utils and be able to use them without importing, but they only work if they are defined in the unnamed package and imported. If the classes are defined in the utils package they generate the error: "5001: The name of package 'utils' does not reflect the location of this file. Please change the package definition's name inside this file, or move the file." Where would I move it to? Perhaps the answer is to make utils a subdirectory of the top level folder, alongside aardvark, but this would not meet my objective as utils would then have a project-specific location. It's no big deal because I can achieve my goal but I'd like to understand what's going on.

 

This topic has been closed for replies.

2 replies

Inspiring
September 2, 2009

In addition to what others said, I suspect based on the error that you may not write the class header correctly. Class header MUST include entire path to the folder where they reside relative to the place where your document class or class you initiate your application.

For instance if the class MyClass resides in a directory c:\projects\myapplication\com\mydomain\utils and given that the top directory for all the classes for current application is com, class header must look as following:

package com.mydomain.utils
{
    public class MyClass
    {

        public function MyClass()
        {
           
        }

    }

}

Inspiring
September 4, 2009

Thanks for the various inputs. I've now clarified things to my satisfaction:


A package is created by defining a class, function etc with a particular package name eg utils, and placing it in a folder with the same name as the package. If using a namespace approach eg package named com.company,utils, then folder structure must be com>company>utils. To use a class from the utils package a classpath must exist pointing to the folder containing utils, or for the namespace example, to the folder containing com. You then use import utils.class or import utils.*.


You can also create a global function, ie in an unamed package, which can be referred to without using import but the classpath must point to the folder containing the .as file, not to the folder above it. The folder must not contain a file with a package definition matching the folder name.


Here's where I was getting confused. If you place a global function in a folder already designated as containing a package via the above proceedure, it will throw the error 5001 described above. But it runs without error if you remove the package name from the other files in the folder, hence clearing any package definition and releasing the folder.

Inspiring
September 4, 2009

If you attempt to import a class or package - compiler will check integrity of package's content. So, compiler bothers with things ONLY if you tell it to (again, via import statement). Thus, if you point compiler to a class that is not written correctly, compiler will let you know that it cannot use the class. If you don't point  - coompiler doesn't care.

In addition, the classes in the same folder can be instantiated directly without import statement. In this case compiler will be aware of a class if you attempt to declare a variable with datatype of this class or create an istance.

Conceptually, before compiling application compiler checks all the POTENTIALLY used code for integrity. Why I am stressing POTENTIALLY is that import statement is not a command to physically take the code and place it into application but just a pointer that, in plain English, says "if I tell you to use class MyClass you will find it in this package." Actual code inclusions occurs only if you declare and/or instantiate variable with this datatype.

September 2, 2009

You want to set your classPath, in Flash, to the top level folder containing your classes.

For example say you have

C:\FlashClasses\AS3

You then set your class path to c:\FlashClasses\AS3

Now within the as3 folder you can organize further - and this is where the packages come in. In my AS3 folder I have:

com

     gmrmarketing

     greensock

     blurredistinction

etc...

A single class within the gmrmarketing folder would then have a package definition of: package com.gmrmarketing and maybe a class name of Water

And in Flash you might do:

import com.gmrmarketing.Water;

var water:Water = new Water();

In your example with a utils folder within your as3classes folder - you would set your class path to c:\as3classes

Then your package would be: package utils

If you classpath all the way to utils, you would have to use an unnamed package...

Inspiring
September 2, 2009

dmennenoh, thanks for your input. So I can use either of these two approaches:
classpath = \as3classes, and package utils{
or
classpath = \as3classes\utils, and package {
Both cases require import utils.*, so what is the benefit in using the first approach which allows the class to be defined in the utils package?

Inspiring
September 2, 2009

I guess it depends on where you place your fla and swf file. if you placed it INSIDE the utils folder and then placed your classes there too, then you can simply use package {  but if you placed you fla and swf inside as3classes OUTSIDE utils while your classes were inside utils, then I guess you should use package utils {