Sharing AS3 Classes with a loaded SWF

I recently hit a strange bug when trying to share AS3 classes between a ‘shell’ Flex SWF and a loaded ‘module’ SWF. When you do this with Flash player 9.0.28, a run-time error is generated: “ReferenceError: Error #1074: Illegal write to read-only property [ClassName] on global. at global$init()”.

I imagine one solution is to prevent the shared classes from being compiled into the module swf. There is info on excluding classes using Flex here. This doesn’t seem to be possible using the Flash 9 public Alpha. Does anyone know how to exclude classes with Flash 9?

8 Responses

  1. JesterXL says:

    Exclude files work in Flash 8, AS2, so I’m assuming they work for AS3 as well. I haven’t tested using the public alpha, though, nor CS3.

  2. Robert Stuttaford says:

    You set applicationDomain in the loaderContext for the Loader that loads the SWF up. This way, it’ll partition the class definitions in the loaded SWF in a separate ApplicationDomain. Read up on ApplicationDomain, there are several options.

    Also, you can use -link-report=Classes.xml as a compiler option (not sure about FCS3) to generate a list of classes in a SWF, and then use -link-external=Classes.xml when compiling the other SWF to have it exclude any classes in the first SWF.

    This XML format is much better than _exclude.xml, it shows you how big each class is, what it extends, and what it depends on.

    That way, common classes can stay in the base SWF, or, if your modules need to be a little more portable, you can exclude the common classes from the base SWF.

  3. Administrator says:

    JesterXL: I assumed that exclude files wouldn’t work with AS3 – I guess I should test that out.

    Robert: the problem with setting the loaded swf to be in a different ApplicationDomain is that then you cannot communicate between the 2 swfs, since the 2 sets of classes are in separate domains.

  4. z says:

    that’s what i do:
    just set the loaded swf to a new applicationdomain,
    and keep the shell class ref in module using Object or other flash global class, you can call the method as usual, but that’s no compiler type check when you are coding.

  5. Administrator says:

    One solution is to use getDefinitionByName() to avoid explicit references to the shared class. This prevents the shared class from being compiled into the module swf:

    —code————
    import flash.utils.*;

    var ClassReference:Class = getDefinitionByName(“com.domain.SharedClassName”) as Class;
    var instance:Object = new ClassReference();

    —code————

    However this is ugly. Excluding the class with a compiler setting would be nicer.

  6. Michael Prescott says:

    I just skimmed the comments, but it sounds like there is no definite answer to the question of whether classes can be excluded from a swf by using an “*_exclude.xml” file when published by Flash CS3. Since I used exclude files extensively in other projects, I have been dissappointed to find they aren’t working, or at least things are working as expected, in Flash CS3. I have ResourceLoader.fla, Resource1.fla, and Resource2.fla. Resource1 and Resource2 have a class named BasicButton compiled into them. ResourceLoader references the BasicButton, but ResourceLoader_exclude.xml contains the following:

    In Flash 7 and 8, BasicButton would NOT be compiled into the ResourceLoader.swf; however, in Flash 9 (CS3) BasicButton is compiled into the ResourceLoader.swf . What is the solution to this? I cannot switch production to using Flex. And, the getDefinitionByName method seems like it would be a horrible solution, with the loss of type checking or clunky, risky usage of casting.

  7. Michael Prescott says:

    … ResourceLoader_exclude.xml contains the following:
    [excludeAssets]
    [asset name=”BasicButton”/]
    [/excludeAssets]

    where [ and ] are

  8. Michael Prescott says:

    In your search for information about exclude files, if you land here first, just move on to http://www.bit-101.com/blog/?p=941 and read “Steve Bond Says:” on “July 26th, 2007 at 1:42 pm”

    It is ALL that I’ve found besides my own tests that proves that exclude files do NOT work in Flash CS3. I sure could use a better solution, and I’m a bit surprised that a previously documented feature of this magnitude would be removed.

Leave a Reply

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