I've Tried Subtlety Before, But I Will Not Anymore...

Ok Folks, here's my first-ever blog, Welcome! One of the things I've always found frustrating is the difficulty I've encountered when I'm trying to find a simple (and I mean S-I-M-P-L-E basic E-Z helloworld 1-minute example) of how to do a particular programming task. Usually, I end up finding very convoluted, over-complex code samples to accomplish a simple task... So, for starters, I am going to start adding my own "101 Examples" which I've created myself as a result of my own Flex application development work. One section of this blog then, will be devoted to creating an ever-growing library of easy-to-understand examples, along with random quotes from songs I like, such as the title of this intro (guess who?) and blog... Stay tuned...

Tuesday, April 8, 2008

Create an SWC from an AS3 Class in Flex

You can compile actionscript class files into binary SWC files for easier distribution and reuse by using the compc.exe tool in the Flex SDK. For instance, the entire Cairngorm framework source code is also available as an SWC, and is easily added to your project by simply copying and pasting the swc into your "lib" folder in your Flex Project (Rather than copying the "com" directory, specifying the global source path, etc... ). So here is a simple example of how to compile a class into an swc and test it in another project:
1. Create a new folder called "swc", and place it somewhere easy to access, such as "d:\flex\swc"
2. Create a simple actionscript class, such as the following "Hello.as" file, and place it in the "swc" folder.
package swc
{
public class Hello
{
public function Hello()
{
trace("hello");
}
}
}
2. Locate your compc.exe file in the Flex SDK. If you are using Flexbuilder 3, it should be located in the "C:\Program Files\Adobe\Flex Builder 3\sdks\3.0.0\bin" directory.
3. Run the compc.exe file by navigating to the same directory via the command prompt, then type the following in RED on the same line (separated here only for readability) :
compc.exe
-source-path=D:\flex [path to your package]
-include-classes swc.Hello [this dot notation must match exactly the same as designated by the package in the class]
-output=D:\flex\swc\bin\myHello.swc [where you want the swc generated and it's name]

4. When you hit return, a folder called "bin" will be created with the swc file inside it. Create a new Flexbuilder 3 project, and simply place this swc into the libs directory. That's it! It is now ready to be accessed via AS3 or through the mxml, such as in the following example:

//instantiated through actionscript
import swc.Hello;
public var myHello:Hello = new Hello;

//instantiated through mxml
xmlns:swc="swc.*"


No comments: