Thu Dec 17 00:35:34 2009
At the time I began learning ActionScript in Flash, which is probably where most of us start, Flex was not something I even considered. At that time, the timeline looked all too comfortable and compiling from classes alone simply looked like far too much hard work. However, all that has changed now and as I wrote in one of my earlier posts, with the use of SVG and FXG(Flex 4),the albeit wonderful drawing tools of Flash are no longer necessary. So with that said, there are no excuses not to try it out.
Before you attempt anything though, get over to Adobe Open Source and download yourself the Flex SDK and save it somewhere you can get your hands on it easily. I'm currently using Flex 4 (Gumbo), which is at the beta stage at the time of writing this post. Those who like stability should grab the latest Flex 3 version. Either will suffice for our purposes.
Once your download has finished and has been unzipped, you should have a folder called flex_sdk_3 or flex_sdk_4, depending on which version you downloaded. Inside this folder, you should find a folder called 'bin' inside of which you will find a file called 'fcsh'. The file 'fcsh' is the Flex Compiler Shell and will be used to compile our classes.
The other thing you are going to need is a file to compile. For your convenience, here is some code that will get you a little 'Hello World' project going. Just save it as Main.as
public class Main extends MovieClip
private var txt:TextField = new TextField();
private var txtFormat: TextFormat = new TextFormat( "Helvetica", 22, 0x333333);
public function Main()
txt.selectable = false;
txt.autoSize = TextFieldAutoSize.LEFT;
txt.text = "Hello World";
txt.setTextFormat( txtFormat );
txt.x = stage.stageWidth/2 - txt.width/2;
txt.y = stage.stageHeight/2 - txt.height/2;
Compiling using Flex Compiler Shell (fcsh) in OSX
Compiling using Terminal is very easy. First open it up. If you have never used Terminal before, you'll probably find it inside the Utilities folder inside your Applications folder.
Once Terminal is open, we need to use it to open up the Flex Compiler Shell. There are three ways to do this on Mac. You could type the location of the fcsh file into Terminal and press Enter. You could drag and drop the fcsh file into the Terminal window and press Enter, or you could just double click the fcsh file.
Compiling using Flex Compiler Shell (fcsh) in Windows
Compiling in Windows is virtually the same process to that of a Mac. The only real difference is that you use Command Prompt instead of Terminal and you open the fcsh jar launcher instead of the fcsh unix executable file.
Whichever way of you choose on whatever OS you use, you should be faced with a Terminal/Command Prompt window that displays the following on it's last line.
If for some reason, you cannot open the fcsh or are not greeted with (fcsh) in Terminal, go get another build. Chances are that another build will work out for you.
If you do see the (fcsh) message, compiling from here is easy. Type the following code and press Enter.
mxmlc -o="The location you want to output to" -file-specs="The localation of you class"
If your Main.as file is on your desktop on OSX and you want to save the swf to the same location, the line above will look like this.
mxmlc -o=/Users/YourName/Desktop/Main.swf -file-specs=/Users/YourName/Desktop/Main.as
Instead of typing the full location, something I sometimes do is type mxmlc -o= and the drag the file I want to compile into the Terminal windows and then change the '.as' to '.swf' and then continue typing, again dragging the class file into the Terminal window after the -file-specs part.
Provided everything was fine with your class, you should now see a .swf file in the output location you entered. If you open that up, you should be greeted with the words "Hello World". Congratulations! It's not really that hard and the advantage to compiling using fcsh as opposed to other methods available is that it shaves a lot of time of the each compile. As long as you don't close the Terminal window, what you compiled previously will still be in the compiler memory and it will simply recompile any changes to your files rather than recompiling the whole thing.
In my opinion, this is a great free alternative to any of the paid software out there. Anyone who hasn't should definitely check this out. For choice of good editors, FlashDevelop (free) seems to be most peoples choice of free editors on Windows, and I personally am a big TextMate (paid) fan and thoroughly recommend it for all types of coding on OSX. As a side note, compiling can be done from inside either of these editors which makes life a lot easier too.