Uniquely Compile AS2 with JSFL – Avoiding Class/Namespace Collision between swfs

AS2 does not have the concept of an application domain. So if you are loading multiple child SWFs into your AS2 project, any classes that the SWFs share will be cached at their first use. The cached definitions will stand in for any following references to the class. Not always good, and depending on your project, it can cause some pretty serious issues and headaches. Need a way around this AS2 class collision issue? Me too, so I made one.

The problem

The FlashPlayer caches classes based on their class paths/namespaces. This means it expects that all definitions of the classpath will be identical throughout FlashPlayer instance. That caching is efficient, but is not always what we want.

For example, pretend we have 2 SWFs that use the com.greenethumb.Blah class. SWF1 was made a loonnng time ago, when com.greenethumb.Blah was weak and ancient. SWF2 is new and uses the amazingly refined and optimized 2.0 version of com.greenethumb.Blah. Cool. When run independently, both swfs work and do their job. However, you have an appicaltion that loads both SWF1 and SWF2. If SWF1 loads first, it runs correctly but when SWF2 loads, OH NO, it can’t use it new fancy version of com.greenethumb.Blah because the FlashPlayer has cached SWF1′s version and is forcing it on SWF2! Wouldn’t it be nice to allow the SWFs to use the class definitions they were compiled with?!

The goal

We want to easily compile our AS2 swfs without worrying that incremental changes to your source files will break other swfs in our project, or require us to retrofit our other swfs to work with the modified code.

The hard way

It SOO simple let’s just:

  • copy all source files to a uniquely named temporary src folder
  • update all the temp src files to reflect their new src package
  • update all the library items with class linkages
  • find all the actionscript in our timeline(s) and update any import statements
  • poke our eyes out after 5 mins of this crap

The easy way

I prefer to make my computer handle such automated stuff. I wrote a JSFL script to handle all this stuff for me.

The script

Here is a zip with the JSFL script and example FLAs . Happy compiling…. Well, as happy as you can be working with AS2…

9 Responses to 'Uniquely Compile AS2 with JSFL – Avoiding Class/Namespace Collision between swfs'

  1. Javascript for the Flash IDE. Its kinda like writing automator scripts or photoshop actions for the Flash IDE… except you write it all by hand :)

  2. Kevin Fox says:

    It occurred to me that I need to run the Unique Compile script on one.fla and two.fla to get the desired non collision, right? I tried that and I still got the “out of memory” error. This was after I shut my computer down, and restarted it, to make sure that I was working on a fresh slate, so to speak. Any suggestions would be greatly appreciated.

  3. Hey Kevin, I actually don’t have CS5. I know for sure this runs on CS3 and CS4. I’ll look into testing with CS5 and/or looking around for issues with ‘ObjectFindAndSelect’ with CS5.

  4. Kevin Fox says:

    Please help, I’m getting a “out of memory” error.

    I opened main.fla in Flash and then clicked on jsfl/compile/Unique Compile.jsfl
    This is what came in the Output window in the IDE


    oldPath: /Users/foxkev/Desktop/dev/AS2_UniqueCompile/src/ — alreadyCopied: false

    The following JavaScript error(s) occurred:

    At line 142 of file “/Users/foxkev/Library/Application Support/Adobe/Flash CS5/en_US/Configuration/Javascript/ObjectFindAndSelect.jsfl”:
    out of memory


    The script makes duplicates of the source files and folders and generates a new swf. So a lot appears to be working normally.

    When main.swf is run, it outputs these traces with the same color boxes (both purple)

    execute (static): two
    execute (instance): two
    execute (static): two
    execute (instance): two
    two loaded
    one loaded
    two — sendEvent: DemoEvent_SomeEvent
    handleSomeEventTwo: DemoEvent_Action :: DemoEvent_SomeEvent
    two — sendEvent: DemoEvent_SomeEvent
    handleSomeEventOne: DemoEvent_Action :: DemoEvent_SomeEvent

    Do you have any idea why I’m getting this?

    I’m using CS5 on a Mac.

    Thank you for diving into this problem and sharing your solution publicly. Before finding your page I was definately in the “- poke our eyes out after 5 mins of this crap” stage. That is f-ing funny!

  5. I don’t see anything specifically related to CS5.

    as an side, if you are seeing the duplicated files, can you open DemoClass.as in one of the generated folders and see if the package name is prepended with a random string.

    this will help track down where the error is occuring.

  6. Kevin Fox says:

    It is not modifying the DemoEvent code. It looks the same as the original one.

    import mx.events.EventDispatcher;
    import mx.utils.Delegate;
    import events.DemoEvent;
    class com.test.DemoClass extends MovieClip
    	public static var ClassName:String = “__Packages.com.test.DemoClass”;
    	public static var registered:Boolean = Object.registerClass(DemoClass.ClassName, DemoClass);
  7. Ravi N says:


    We found this link on
    http://greenethumb.com/file_download/33/gthmb_as2_converter.zip on stackoverflow page (http://stackoverflow.com/questions/850839/as2-to-as3-migration-handling-code-left-on-buttons)

    Can you point us to the right link, if the above is broken

    Thanks for your help


  8. Jonathan Greene says:

    Hey Ravi, thanks for spotting that dead link! It was a causality of the WordPress migration. To be clear, the .zip you are looking for is for converting AS2 to AS3, not uniquely compiling AS2 swfs (which this thread is about).

    Anyway, the new link is here:


Leave a Reply

Your email address will not be published. Required fields are marked *