06OctCreate AIR applications out of your old AS2 code

If you have some old AS2 code laying around that you'd like to take into the desktop, here you have a quick tip that might save you hours of re-coding those AS2 classes into AS3.

In my case, I had an AS2 project completed that I had long ago converted into an executable with Screenweaver. I needed to change a couple of things, and it was then when I realized that Screenweaver was obsolete and would not run in Vista.

Of course AIR is the "desktop publisher" of choice right now, but AIR supports only AS3. The solution comes (using Flex) with an easy SWFLoader, that simply performs an old loadMovie, effectively loading your AS2 movie into AS3. Create a wrapper AS3 class that simply loads your AS2 movie, and create your application with AIR.

You'll need to create a WindowedApplication class to substitute the default class created by Flex.

AS2AIR.as

package com.domain
{
    import mx.core.WindowedApplication;
    import mx.controls.SWFLoader;
   
    public class AS2AIR extends WindowedApplication
    {
        public var swf:SWFLoader;
       
            public function init():void
            {
                this.swf.source = "My_Old_AS2_Code.swf";
            }
    }
}

And the MXML file contains just that SWFLoader.

AS2AIR.mxml

<xml version="1.0" encoding="utf-8"?>

<app:AS2AIR
    xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:app="com.domain.*"
    initialize="init();" width="800" height="600"
    layout="absolute" xmlns:ns1="*">

<mx:SWFLoader id="swf" scaleContent="false" width="100%" height="100%" x="0" y="0"/>
   
</app:AS2AIR>

You can, of course, indicate the source file direclty in the MXML file, but if you need to manage some communication between your AIR application and your AS2 movie, you'll have to deal with your own AS class.


  1. 1 Yisus09 Jan 2008

    Nice tip!

    but where do i place the AS2AIR.as file and how do i import it? ¬¬

    thanks!

  2. 2 omar08 Apr 2008

    AS2AIR is a subclass of WindowedApplication that is used as the main class that creates the application. In the example above I am using app:AS2AIR, instead of the common mx:WindowedApplication.

    In AS3 the location of the file must correspond to its namespace. In this case the namespace is defined in AS2AIR.mxml where it says: xmlns:app="com.domain.*".

    So the location of the AS file must be com/domain/AS2AIR.as. This is of course an example and you can change the location of the file and the definition of the namespace to fit your needs.