Using Flex compiled code within Flash!

Why did I put effort in combining Flash and Flex generated swf’s?

A major AS 3.0 obstacle for me is the fact that the flex RPC sources are not available. This means we can only use rpc.swc supplied within the Flex framework or use a third party library. Another big thing I’ve been messing around with is the idea of using cairngorm within the Flash authoring environment. As we all know cairngorm depends heavily on databinding, one can simulate databinding using simple macros and a preprocessor or one can let the flex compiler do all the hard work and embed the flex generated library into flash. I’ve been trying hard and succeeded in both, just read on…

As I was messing around with flex (compc) generated swc’s and things like playerglobal.swc, flex.swc, rpc.swc, etc… I came to found out that Flash actually compiles code against swc’s!! Yes, that’s compiling, this means NO linking! So, the linking part has to be done manually… Do you remember ‘intrinsic classes’ back in the good old AS 2.0 days? This result is the same, but you’re not dealing with exclusion xml’s and or the ASI files!
In short, what do you need to do?:

  1. First create a class that will import your needed dependencies (mx.*, whatever you need!)
  2. Now create your component library from your class file with ‘compc’, for ex:
    ‘compc -source-path . -include-classes FooLib -output=./example.swc’
    You should now have an swc file which you can use to compile against. In the flash authoring environment the swc is only used for type checking, think of it as an intrinsic package!
  3. Now you’ll compile the library that you’ll dynamically load to access your classes:
    mxmlc -source-path . -o ./bin/library.swf ./FooLib.as
  4. Before you’re ready to compile against your newly created library you need to load the library at runtime. As far as I can tell there are two options, my favorite is to link against it as a shared library. This allows all your swf’s to run quickly without any actual code! First you make a link to the library and when you’re done you put the movieclip on the first frame of your application. The other option (which is excellent as a preloader btw.) is to use a standard flash.display.Loader. (Just don’t forget to set the ApplicationDomain in the loadercontext or you’re classes will not be accessible!)
  5. And finally you’re ready to compile your Flash classes against the classes flex compiled for you.

As a proof of concept I’ve converted (read quick & dirty) the cairngorm cafetownsend mxml views to Flash. The views have dependencies on cairngorm and the flex framework (mx.rpc.*, mx.validators.StringValidator, etc..), but they compile without any of these classes in the path just because the swc sits besides the fla! You can download this package here. After you’ve downloaded the flex sdk to your favorite location you need to run ‘compile.cmd’ (put the flex sdk ‘bin’ folder to your path, or you need to edit compile.cmd and at the path in front of compc and mxmlc), this will automate step 2 & 3. Now you can open cafetownsend.fla and compile without any errors.

If you’re only interested in getting webservices support in Flash then download the examples here.

Now let’s use cairngorm within our Flash authoring environment :-)

For ‘Jon Bradley’ from the FlashCoders maillist I tested some flex controls. And yes, that’s possible as well although you need some style classes initialized before adding controls to your view. My first attempt involved custom flex initialization, download that one here. My second attempt has a better workflow: you create a basic flex application which is compiled against your library.swc. This basic flex application is used for initialization when you want it to! The trick involves the overriding of the flex application. If you want to look into this method further then download a working example here.

As far as I (or google) can tell this is the first proper solution to use your flex classes within Flash!

[Edit1]As Geoff van den Ouden found out, this solution does not work instantly with the Flex 3 SDK! You need to add “-compute-digest=false” to as an extra parameter to compc. Or you should download flex 2.0.2.[/Edit1]

[Edit2]Julian Kussman got AIR crashing while using this method. It turns out AIR doesn’t like the shared library option. You can download a working AIR rpc example here.[/Edit2]

Good luck!

Simple Actionscript Preprocessor

I was looking for a ’simple’ C preprocessor for actionscript. The best thing out there which is out of the box compatible with actionscript is “EnLarge” from Asual.

But even EnLarge is to complicated for me! I don’t want to make an ant build for every little thing I do. All I want is a preprocessor that takes an input folder and processes every single file to a specified output folder. So I took a peek on google and found “GPP“, which is a general-purpose preprocessor ‘almost’ perfect for my needs. Yes, ‘almost’ perfect because it’s missing some of my fundamental needs to process a folder for ex. So after some small modifications I’ve recompiled GPP to fit my needs. Download my binaries here, and get my patches for GPP 2.24 here.

GPP now supports the following syntax:
gpp.exe –source .\src_p –dest .\src –nocurinc –stdinclude .\macros

We’re using this in FlashDevelop using a shortcut key (F11) within the FlashDevelop menu, if you want this as well then copy gpp.exe to your FlashDevelop tools folder and modify MainMenu.xml with the following:

<button label="Preprocess src_p" click="RunProcess" shortcut="F11" tag="$(Quote)$(BaseDir)\Tools\gpp\gpp.exe$(Quote);--source $(Quote)$(ProjectDir)\src_p$(Quote) --dest $(Quote)$(ProjectDir)\src$(Quote) --nocurinc --stdinclude $(Quote)$(BaseDir)\Tools\gpp$(Quote)" />

to super or not to super

Sometimes there are reasons for not calling the base class constructor. A little trick in AS3.0 to not super() your base class is the following line of code:

if (0) super();