Coding in Flex Builder, Compiling in Flash Authoring
Nostalgia and complaining
Years ago, before AS3 and Flex 2 were on the scene, I was a Flash developer. I dabbled in other things like PHP, JavaScript and the horrific “classic” ASP, but Flash development with AS2 was always more interesting to me. I worked with great designers who of course used Macs, while me and the rest of the developers were on PCs. (Don’t worry, I use a Mac now.) In order to preserve the designers’ fonts and hand crafted animations, we taught them how to assign linkage names to their symbols and would have them deliver a .swf to us that had nothing on the stage, allowing us to pull symbols out of the library as we needed. Using the fantastic MTASC compiler and an editor like PrimalScript, our class-based AS2 code would be injected into the designers’ .swf, bringing the site/game/whatever to life. Ahh, those were the days.
Fast forward to last week when I was assigned to my first ever non-Flex AS3 project and discovered that developing like we used to in Flash (tying AS classes to library symbols) is pretty painful these days. Or at least it was for me, until I constructed a very convenient workflow that you can probably guess at based on the title of this post. Now, I should probably explain what I mean by painful. You can write your AS3 classes in Flash authoring, and the process is very straightforward; just create classes whose names match the symbol linkage names and you’re done. Just like AS2. However, Flash authoring’s code editor is absolutely abysmal. I mean, I knew it was bad, but I was actually shocked at how bad it is. It won’t even create import statements for you when you create properties! If you have used Flex Builder or any other decent editor for more than 10 minutes, this is completely unacceptable. You could also write your code in Flex Builder, save, switch to Flash authoring and then test movie or publish from there. Honestly though, do you really want to do that 500 times a day? Me neither.
So, what is this better way you ask? Well, we use one improvement that did come with Flash authoring’s support of AS3, which is the document class construct. This means that you simply create a custom class to represent your application, make sure it extends MovieClip, and tell the .fla to use it. We don’t need any code at all in our .fla, unlike back in the day. This essentially means that your custom class is your root timeline, and adding display objects to it is like adding them to the stage.
Coding in Flex Builder
Since I use a Mac, Flex Builder and FDT are the only realistic options for AS3 editors, as far as I know. If you are on a PC, FlashDevelop is also a great program. Now, Flex Builder allows you to create ActionScript projects. In contrast to a Flex project, I assumed an ActionScript would be devoid of any reference to Flex SDKs, but this is not the case. Your ActionScript project will still reference an SDK in the compiler settings and in the build path dialog, but that is OK. Since you don’t reference any Flex classes, you will not be pulling the framework into your .swf. I think it is only referenced so Flex Builder knows where to find mxmlc, but I am not completely sure. In order to use library symbols as class instances, you will need to create corresponding classes (that extend MovieClip) for them that match the name specified in the their linkage field. Whether you need to add custom code or not, the classes must exist in order for you to reference them in Flex Builder. To reference their properties, like child text fields or buttons I declare those properties in the class and turn off “Automatically declare stage instances” in the .fla’s publish settings.
Compiling in Flash authoring
OK, none of that was revolutionary but now comes the hard part. How do you compile/test your project without having to switch back to Flash every time? The first piece of the puzzle is to use the flashcommand Python script that Mike Chambers posted a long time ago. (It is only for OS X in its current state but I would assume creating a PC-friendly version wouldn’t be too hard.) All this really does is open Flash if necessary, and compile the .fla(s) you tell it to, by writing JSFL to a temporary file and then running a line of AppleScript to execute the JSFL code. That is really great, but switching to Terminal every time I want to compile is no better than switching to Flash. I needed something I could trigger from within Flex Builder, so I naturally turned to Ant. All you need is a super simple build file, like this:
As you can see, it just sets up a couple of properties and asks Python to run the flashcommand script. The -e argument tells flashcommand to include doc.exportSWF() in the generated JSFL, which will publish the .swf to the location specified in the -o argument. If this is all you need to do, but want to then launch the newly created file in Flash Player, you can add the following to your build.xml file.
That was my initial approach but it makes viewing your traces kind of a pain. The easiest way I found was to tail your flashlog.txt file but that still kind of sucks. Its especially bad because a LOT of .swfs on the web still include trace statements, meaning your Terminal is flooded with statements generated by every banner ad running in your open browsers.
What I ended up doing was to edit flashcommand so that the -e argument also adds fl.getDocumentDOM().testMovie() to the generated JSFL, causing Flash authoring to execute a test movie action. I also added activate to the end of the generated AppleScript so that Flash’s test movie window would be brought into focus and ready for interaction. This allows you to use Flash’s built in Output panel to view your trace statements, and your trace statements only. (I tried making the test movie option a separate command line argument but couldn’t get it working correctly. If any Python hackers out there want to show me the way I’d be much obliged.)
Set up a keyboard shortcut in Flex Builder for running Ant and your workflow is almost identical to a Flex or code-only ActionScript project. Your .swf will be generated and published to whatever location you’ve specified, and will also be opened in Flash authoring for visual inspection and testing. I am probably not the first to come up with something like this, but I didn’t find any good links on the subject and other people expressed an interest in hearing about my workflow. If you have suggestions on further improvements (like how to use Flex Builder’s debugger for these projects) I would love to hear them. Regardless, I hope this is useful to other coders who find themselves needing to develop AS3 projects that associate custom classes with library symbols.
If you’d like to replicate this setup, you can download my modified version of flashcommand.
Enjoy!
You can leave a response, or trackback from your own site.
But what I want — and I’m still shocked that Adobe hasn’t provided this out of the box yet — is to use Flex Builder as my debugger (or profiler) as well. Why, Adobe, why can we not do this?
I’ve fortunately avoided having to use the Flash authoring tool as my “build environment” (except for a few mini games — and it was painful there as well). In fact, I’ve gone as far as to completely develop a game inside of FB and then, once finished, build a FLA that references it (so that artists could later skin it).
The approach I’ve ended up with that works best is treating Flash as just a vector animation and drawing tool, just like Photoshop/Fireworks for raster assets, loading or embedding the SWFs. It’s a shame, because there’s a lot of power when the artists and coders can properly work together, both inside their best tools. Maybe we’ll see that finally with Flash Catalyst.
Lee Brimelow, Adobe, did a series on Pimp My Eclipse where it built a set of plugins for Eclipse & the Flex Builder plugin that allowed you to test Flash from within Eclipse. It works really well. http://theflashblog.com/?cat=45
Like Troy, I’ve started to get out of having Flash as my build environment. I’m using Flash to export SWCs of just the movieclip assets. In an AS3 project, I then extend those SWCs to add any necessary logic for that assets.
Hi John,
Can you explain what you mean by “extend those SWCs”? I am interested to hear more.
Thanks!
Ben
Nevermind, I have it figured out, update to come. Thanks for the tip!
[...] couple of weeks ago, I posted about a workflow I had devised that allowed me to utilize Flash library symbols but still write my [...]
I’m with Troy. Compiling in Flash IDE is next to worthless without the Flex Debugger. I use the debugger constantly as I’m developing. I hardly ever write trace statements anymore because of it.
Hi Adam,
I agree. You may be interested in the follow up post I created at http://www.benclinkinbeard.com/2009/09/coding-in-flex-builder-compiling-in-flex-builder-a-better-flash-workflow/.
Ben
You can use the debugger in Flex Builder for apps compiled with the Flash IDE. Be sure to select “Permit Debugging” from the publish settings. You may want to unselect it for production as it does add to the size of the file. Check this link:
http://bigbadcode.com/2008/04/10/flexbuilder_for_flash/