Sharing ActionScript Classes
By default when you compile ActionScript, any classes you attempt to use will automically be looked for in the current directory; you don't need to use import. For example, if you create ShareSingle.as which attempts to use a Circle class, the Flex compiler will look in the same folder for Circle.as. For developing isolated components or projects this typically works out just fine.
You can also share source code from a common directory between projects by adding it to the flex-config.xml file under the <source-path> node as a <path-element> entry. This file is located under the frameworks sub-directory of the Flex 2 SDK on your computer; for example if you're using Flex Builder it will probably be in C:\Program Files\Adobe\Flex Builder 2\Flex SDK 2\frameworks\. Any folder path you put in that configuration file will add it to the search list. Thus when you build an ActionScript file, the compiler will look in the currrent directory and then any directories you listed there in order to find other classes.
It's also fairly easy to create a package hierachy so you can specify what you want to include via import statements. This also breaks your source files up into multiple directories if you're into that kind of thing. In order for this to work you'll first need to specify a package name at the top of your code rather than leaving it blank. Next create folders whose names match the ones in your package string, with periods representing separators for sub-folders. For example, the source for this:
Would be placed in a hierachy like this:

When you're ready to use classes from this kind of source-based package (e.g. not in a JAR/ZIP), you'll use an import statement in your project and make sure you have the base directory (C:\Stuff\ObremSDK in the above example) specified as a <path-element> in flex-config.xml.
Lastly, I want to mention an oddity with the Flex (MXML) compiler when it comes to errors for classes it could not find. If you call import on a package it cannot find, this gets reported last; after any "Type was not found" errors. I'm guessing they assume people are going to be reading failures from the bottom up, but I'd just like to throw this warning out there for those like me who are used to reading top-down.
You can also share source code from a common directory between projects by adding it to the flex-config.xml file under the <source-path> node as a <path-element> entry. This file is located under the frameworks sub-directory of the Flex 2 SDK on your computer; for example if you're using Flex Builder it will probably be in C:\Program Files\Adobe\Flex Builder 2\Flex SDK 2\frameworks\. Any folder path you put in that configuration file will add it to the search list. Thus when you build an ActionScript file, the compiler will look in the currrent directory and then any directories you listed there in order to find other classes.
It's also fairly easy to create a package hierachy so you can specify what you want to include via import statements. This also breaks your source files up into multiple directories if you're into that kind of thing. In order for this to work you'll first need to specify a package name at the top of your code rather than leaving it blank. Next create folders whose names match the ones in your package string, with periods representing separators for sub-folders. For example, the source for this:
Circle.as
package as3.shapes
{
import flash.display.Sprite;
public class Circle extends Sprite
{
public function Circle(x:Number,
y:Number, radius:Number,
color:int)
{
graphics.beginFill(color);
graphics.drawCircle(x, y, radius);
graphics.endFill();
}
}
}
Would be placed in a hierachy like this:

When you're ready to use classes from this kind of source-based package (e.g. not in a JAR/ZIP), you'll use an import statement in your project and make sure you have the base directory (C:\Stuff\ObremSDK in the above example) specified as a <path-element> in flex-config.xml.
Test.as
package
{
import flash.display.Sprite;
import as3.shapes.Circle;
public class Test extends Sprite
{
public function Test()
{
addChild(new Circle(25,
25, 10, 0xFF0000));
}
}
}
Lastly, I want to mention an oddity with the Flex (MXML) compiler when it comes to errors for classes it could not find. If you call import on a package it cannot find, this gets reported last; after any "Type was not found" errors. I'm guessing they assume people are going to be reading failures from the bottom up, but I'd just like to throw this warning out there for those like me who are used to reading top-down.


