Creating AIR projects with Actionscript
If you’re using Flex Builder 3, and you want to create an Actionscript project which will eventually deploy as an AIR application, you’ll quickly find that this option isn’t available when using the Actionscript Project Wizard. There are plenty of posts out there detailing a workaround (for example, see here).
I followed the workaround steps and it seemed to work OK, but when I first published my application… nothing happened. A little icon appeared in my dock, mysteriously titled adl (which I’ve since discovered stands for the AIR debug launcher), but no windows of Flash-based goodness appeared anywhere. After much digging around, I came across this entry in the Adobe Bug System and found the answer buried away in the comments.
When I’d previously created Flex-based AIR projects, I hadn’t really appreciated that the Flex framework was handling all of the window-creating shenanigans on my behalf. Just by making my root MXML tag a WindowedApplication, instead of a “normal” Application, the main AIR window was being created and activated automatically. When you’re not using the Flex framework, you have to do this stuff yourself.
As it turns out, the two most important classes involved in this process are flash.display.NativeWindow and flash.display.NativeWindowInitOptions. A NativeWindow is essentially the window you see on your desktop when you run an AIR application. This includes the menu bar (if any) and the system chrome (if any).
To successfully create and launch a window in AIR, you’ll need to create and activate a NativeWindow object, and then add your Document class (or some Sprite, at least) to that Window’s stage, accessible through the NativeWindow’s stage property. The code will look something like…
var windowOptions:NativeWindowInitOptions = new NativeWindowInitOptions();
// create the window
var mainWindow:NativeWindow = new NativeWindow(windowOptions);
// activate the window
mainWindow.activate();
// adds "this" to the stage of the Window we have created
mainWindow.stage.addChild(this);
This code will create and activate an AIR window with default options, and add someSprite to the stage of that Window. From then on, you can continue with your code just as you would in a traditional Flash application and all will work as expected.
I’ll post more about the NativeWindow and NativeWindowInitOptions classes at some point soon….
(Update: Check out this post for more information about the NativeWindowInitOptions class)
Tags: AIR, AS 3.0, Flex Builder