Package and File Organization - Flash 9
Well I said there would be more Flash 9 Tutorials, and well here you go! This is on (as if you have’nt guessed) the Package keyword and file organization in Flash 9. A few people I’ve talked to have had a abit of trouble trying to get this working so it warrants a Tutorial. So anyway lets get in to it!
Okay first of all you need to realise package really does make your life easier, especially in the long run when you want to remember where you placed your class to draw a circle for example. First before we jump in to anycode I think it would be a good idea to setup your classPath correctly. Your classPath is where flash is going to look for your classes that you have created. So we are going to need somewhere to keep all of our classes, mine is D:\Stuff\Flash Classes\ Choose somewhere thats appropriate for you. Now this is where ALL your classes will be kept from now on.
A common convention between developers is to have a folder layout here such that: com\\ For example my class layout would be: D:\Stuff\Flash Classes\com\deviant\threeD since my nick is Deviant and the package is my 3d package. Something I feel I should point out at this time is that the package naming convention is it should start off with a lowercase letter, thus deviant rather than Deviant and threeD rather than 3D. This should all seem familiar to people who have used Java packages before or the AS2.0 package system, In this respect nothing has changed in the folder layout for packages.
Now we have our class layout, so what we need to do now is tell Flash where our classes will be. This pretty simple, go to Edit > Prefrences (Ctrl + U for short) on the left hand side choose actionscript in the list box, then click on “Actionscript 3.0 Settings…” And you should get a box similar to:

Now you may have to press the + button and then fill in your own classPath, but I am sure you can manage that :) The other two entries in here are . and $(LocalData)/Classes. The . is to tell the flash compiler to look for classes in the current folder of your flash file, which makes sense, sometimes you dont want packages just a small as file to handle a small thing. The $(LocalData)/Classes is the path to where Flash holds all its standard classes for example Movieclip.
Now Flash knows where your classes are, and we have an efficient file structure that makes sense to not only us but to anyone else who you may need to use your classes for example Clients. Now we need to understand what the package command actually does! Well to put it short it tells you and the flash compiler where this file should go, and where you should look for it. So lets look at some code:
package com.myName.myPackage
{
public class A
{
function A()
{ trace(”A was Created”); }
}
public class B
{
function B()
{ trace(”B doesnt want to get out of bed today”); }
}
}
So what has that told us? It says stright away that this class is located in our classPath\com\myName\myPackage\ directory so we know exactly where it is, it also tells us its part of that myPackage. That also happends gives us access to ALL the subclasses within myPackage. Essentially its like having an import com.myName.myPackage.*; but you dont have to write that! Its to do with the scope of packages if your within a package it makes sense for you to be able to see all the other classes that belong to that package, and thats what is happening here.
Now another thing you might notice is that I placed 2 classes within the same .as file. You might be thinking “oooh you cant do that!” well actually I agree with you… sort of. You CAN do that, just dont. It promotes very very bad practises, and will make your code less manageable. In the end whats one extra file to you? It makes more sense to have one class per file, then you dont have to remember which class you put in which file, because your file name will be the same as the class. Just one less thing to worry about. Apart from that your forcing flash to load classes, when you may not use all of them. This will cause your compile time to increase (a minor problem but still an unnecessary one). But for those of you who might want to do this (silly mad insane people obviously,) heres how. First of all your .as file needs to be the same name as atleast one of the classes inside it, otherwise your flash compiler wont be able to find the file. Now if you want to use any of the classes within the file you need to import the class with the same name as the file. For example with the fake class i did just above, say I called it B.as and I wanted to access my class A. I would have to do:
import com.myName.myPackage.B; import com.myName.myPackage.A;
Technically the order you call the packages in doesnt matter, but you see you have to import B aswell or else it wont know where to look for A. But hopefully you wont be tempted to try and do things like that because of all the reasons I stated before. So now you know all that go have fun with your packages!
on July 21st, 2006 at 3:18 pm
“Just one less thing to worry about. Apart from that your forcing flash to load all of those classes in that file in to memory, when you may not use all of them.”
This statement seems wrong to me… From the AS3 docs for the ‘import’ directive:
“If you import a class but do not use it in your script, the class is not exported as part of the SWF file. This means you can import large packages without being concerned about the size of the SWF file”
Do you have information to the contrary?
on July 21st, 2006 at 4:25 pm
Ah okay, slight mis-interpretation there. Yes your right the classes arent exported if they arent used, but the compiler still will have to check if that class is not being used. That increases compile time only by a miniscule amount but still.
I will edit it now so it is more clear :)
on July 23rd, 2006 at 4:20 pm
Just for the fun of it, I just created included 50 classes to see if it had any change on compile time. The result was not enough for me to notice.
on March 7th, 2007 at 10:46 am
I can’t seem to make this work. I am getting compiler errors whenever I try to use ActionScript3. Specifically, it says it can’t find UIComponent. If I put $(LocalData)/Classes in the classpath it gives a syntax error in what appears to be an ActionScript2 version of UIComponent. On my system, $(LocalData)/Classes is C:\Program Files\Adobe\Flash 9 Public Alpha\en\First Run\Classes which appear to all be ActionScript2 classes, and my ActionScript3 code is not compiling. Any ideas?