Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 03-01-2008, 09:29 AM   #1
GordonG
Senior Member
 
Join Date: Feb 2008
Posts: 326
Default packages vs directories?

I get the impression that packages can be used to hold more than one class - so rather than putting all my classes into separate .as files, I can put them into one and import them with one statement using the wildcard * . ( is this true? )

In the Flash Help where it describes creating packages, I read this:
Quote:
ActionScript 3.0 allows you to include multiple classes in one source file, but only one class in each file can be made available to code that is external to that file. In other words, only one class in each file can be declared inside a package declaration.
How do you interpret that seeming contradiction? Why would there be this wildcard import feature if you can only access one class in the package - "only one class in each file can be made available to code that is external to that file" ?

Thanks for any help,
Gordon
GordonG is offline   Reply With Quote
Old 03-01-2008, 04:08 PM   #2
CTACEG
Registered User
 
Join Date: Mar 2008
Posts: 2
Default

GordonG,
the impresison you get about packages containing multiple classes is absolutesly right, but each .as file cannot hold more than 1 class.

For example:
there is a package aaa.bbb.ccc, containing classes 1,2,3. Ther directories in this case would be aaa/bbb/ccc/1.as, aaa/bbb/ccc/2.as, aaa/bbb/ccc/3.as.

Then in case you need to import your classes you would use:

import aaa.bbb.ccc.1
import aaa.bbb.ccc.2
import aaa.bbb.ccc.3

Wildcard in the form of '*' allows you to skip these repetitions and import ALL classes contained within the package. So in case you need to import all classes(1,2,3) you'd rather use
import aaa.bbb.ccc.*;

Hope this helps.
CTACEG is offline   Reply With Quote
Old 03-01-2008, 04:57 PM   #3
GordonG
Senior Member
 
Join Date: Feb 2008
Posts: 326
Default

You are telling me, by virtue of a file residing in a directory it is in a package? The package corresponds to the directory name? Then what is the "package" statement? Further, I see it again and again that using a wildcard doesn't actually import all the class files in a directory.

So I am still confused - is simply using a directory structure the same as a package?
GordonG is offline   Reply With Quote
Old 03-01-2008, 05:47 PM   #4
CTACEG
Registered User
 
Join Date: Mar 2008
Posts: 2
Default

Yes, package name does correspond to the directory name. It is done to make developer's life a little easier. Packages help you split your classes by functionality.
Imagine a project with 100s classes all residing within a single directory.

And by 'importing' a class we make it available for use in our current document.
Try creating sprites and drawing lines and squares without importing 'flash.display.Sprite' and 'flash.display.Graphics' classes. But then you could use
import flash.display.*;

Note: this is just an example.
CTACEG is offline   Reply With Quote
Old 03-02-2008, 12:05 PM   #5
GordonG
Senior Member
 
Join Date: Feb 2008
Posts: 326
Default

This still isn't quite right. I know why we use the import statement. I understand the general principle.

1. What is the Package statement for?

2. Why are there times when using: import packagePath.* does NOT import all the classes in the directory path?
GordonG is offline   Reply With Quote
Old 03-02-2008, 05:17 PM   #6
Dark Vyper
Registered User
 
Join Date: Jan 2008
Posts: 11
Default

Hi,

CTACEG's comment about only one class being in an AS file isn't entirely correct. An AS file can have an many classes as you want - however only one can be declared inside the package: the rest have to be declared outside of the package and are only visible to the class inside the package.

Also, what examples have you seen where using the wildcard symbol doesn't import all of the classes in the directory path?
Dark Vyper is offline   Reply With Quote
Old 03-02-2008, 06:45 PM   #7
GordonG
Senior Member
 
Join Date: Feb 2008
Posts: 326
Default

I've got a papervision3D sample I'll dig up, but the wildcard simply did not import all classes. I had to explicitly name the class I was trying to import - or the compiler threw a Type not found error when I tried to instance the class. And I've seen it more than once, with other examples as well.

I believe there is some nuance in all this that is not adequately explained. For example, merely placing a class file inside a directory does NOT make it available for import - or maybe accessible after import. One also has to include either some kind of package directive, or name the directory structure in the class declaration, or something...

That's what I'm not getting a clear explanation of, yet...

Last edited by GordonG; 03-02-2008 at 06:59 PM.
GordonG is offline   Reply With Quote
Old 03-02-2008, 10:25 PM   #8
h00d
Registered User
 
Join Date: Dec 2007
Posts: 78
Default

Packages are pretty simple ...

You can avoid naming conflicts by placing your classes into a package specific to your application (com.app.pkg). Because two classes can have the same name, but live in different packages, you are required to import the classes you mean to use.

You can define more than one class in your class file, but only one of them can be declared inside of the package (hence the need for the package declaration).

Packages are also a way of expressing (in code) that the classes contained within them are related in some way.

In my opinion using * is a bad habit. Explicitly importing the classes you are using "reads" better :-P for me anyway.

Last edited by h00d; 03-02-2008 at 10:43 PM. Reason: sp311ing
h00d is offline   Reply With Quote
Old 03-03-2008, 01:29 AM   #9
GordonG
Senior Member
 
Join Date: Feb 2008
Posts: 326
Default

I understand the simplicity, the idea, the function, of a Package. The question is about the implementation, and why it behaves the way it does, sometimes flying in the face of logic.

here's a excerpt from Flash Help on packages:
ActionScript Code:
package flash.xml {       class XMLDocument {}       class XMLNode {}       class XMLSocket {} }

This is NOT in line with the replies in this thread - I see more than one class declaration in side the package directive. Ignore that for a moment: h00d writes,
Quote:
You can define more than one class in your class file, but only one of them can be declared inside of the package (hence the need for the package declaration).
He is implying that I can have a class declared inside the same file, in the same directory, but it is not part of the package...huh? I thought everyone is telling me the package is the directory is the package is the directory...

This is what I am trying to get a clear understanding of, but much is contradicting the explanation provided my the Flash Help description.

If I simply drop an arbitrary .as file into a directory, does that make it part of a package? Or must I modify that .as file to make it part of a package?
GordonG is offline   Reply With Quote
Old 03-03-2008, 02:32 AM   #10
lordofduct
Senior Member
 
lordofduct's Avatar
 
Join Date: Feb 2008
Location: West Palm Beach, FL
Posts: 3,872
Default

That statement was describing something different...

Quote:
Although moving the legacy XML class into a package is a good first step, most users of the legacy XML classes will import the flash.xml package, which will generate the same name conflict unless you remember to always use the fully qualified name of the legacy XML class (flash.xml.XML). To avoid this situation, the legacy XML class is now named XMLDocument, as the following example shows:

package flash.xml
{
class XMLDocument {}
class XMLNode {}
class XMLSocket {}
}
it's a written out description of the contents in the flash.xml package.

You can have multiple classes inside of one source file, but only 1 can be visible to the outside of that sourceFile. For instance in this scenario I have a package with a visible class called "subClass". In the source file I have another class called "internalClass" that can be created from with in this source file... but ONLY in this source file, that is all.

Code:
package {
	
	public class subClass {
		public function subClass() {
			trace ("somethingOrOther");
			
			var a:internalClass = new internalClass();
		}
	}
}
	
class internalClass {
	public function internalClass() {
		trace("another class");
	}
}
So if in my main class document for my project I create an instance of subClass like so:
Code:
package {
	import flash.display.Sprite;
	
	public class main extends Sprite {

		public function main():void {
			var test:subClass = new subClass();
		}
	}
}
the output traces:
Quote:
somethingOrOther
another class

...

As for the package system. It's a way to manage large numbers of source files that have relations to each other. The package name refers to the directory it exists in, with in the directory with all its different classes (in their own .as files) you can define a class to be public to the entire library (by denoting the class public), or only public to classes that exist inside its own package (internal class). This way if you have a class that depends on and should only be created by another class in the package you make it internal; this way when utilizing the library you don't go and create a class that can't be managed outside of its package.

For instance, in the APE physics engine there is the class "CollisionDetector". This class has no use to anyone on its own and is ONLY used by the APEngine itself. With out the APEngine this class wouldn't function... so it remains internal and only used by the APEngine class.

...

more interestingly if we go back to the initial part of this post. You can create the visible class like before... and then a bunch of classes that are only inside of this source file. Then upon cronstruction of the instance you pass a parameter in that denotes what kind of class it is of the marriad of classes with in it...

kinda weird and I've yet to find a use for it personally... but neat none the less. Check here all the way at the bottom of the page:
http://livedocs.adobe.com/flash/9.0/...fV3/Class.html

Last edited by lordofduct; 03-03-2008 at 02:57 AM.
lordofduct is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
max length of path to imported packages asinning ActionScript 2.0 1 11-13-2007 10:20 PM
Flash Components for Flex compiler that are in packages? sys2074 ActionScript 3.0 7 11-13-2007 02:09 AM
Duh Basics: Classes and Packages Gimzani ActionScript 3.0 1 10-31-2007 01:53 PM
dynamically searching for directories in directories tryberius ActionScript 2.0 2 10-03-2007 02:01 AM
count (or get a list of) directories martovarot Projectors and CDs 5 01-04-2007 04:50 PM


All times are GMT. The time now is 04:35 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.