13

When you load a SWF into another, the loader SWF can get specific definitions from the loaded SWF using ApplicationDomain.getDefinition(name:String). For example:

package 
{
    // ... imports

    public class SWFLoader extends Sprite
    {
        private var loadedAppDomain:ApplicationDomain;

        public function SWFLoader()
        {
            var request:URLRequest = new URLRequest("test.swf");
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onTestLoadComplete);
            loader.load(request);
        }

        private function onTestLoadComplete(event:Event):void
        {
            var loaderInfo:LoaderInfo = LoaderInfo(event.target);
            loadedAppDomain = loaderInfo.applicationDomain;

            // Here we can get ANY defined symbol (class, namespace or function according to Adobe Flash help)
            var someSymbolClass:Class = Class(loadedAppDomain.getDefinition("SomeSymbol"));
            var someSymbolSprite:Sprite = Sprite(new someSymbolClass());

            addChild(sprite);
        }
    }
}

How can I get all of the definitions in a SWF, without specifying each explicitly?

Jeremy
  • 1
  • 85
  • 340
  • 366
Lucas Gabriel Sánchez
  • 40,116
  • 20
  • 56
  • 83

4 Answers4

14

As of Flash Player 11.3, you can use ApplicationDomain.getQualifiedDefinitionNames().

See the official documentation for the method and this blog post about the Flash Player release.

Mark
  • 176
  • 4
  • 10
  • Beautiful. At last Adobe added this functionality :) – Lucas Gabriel Sánchez Oct 24 '12 at 13:48
  • As of 3/31/2014, the method is not actually documented. There is no mention of `getQualifiedDefinitionNames` at the documentation link for ApplicationDomain. The method does, however, work, and it returns a `Vector.` of the classes defined in that particular ApplicationDomain. Also note that it doesn't return definitions of parent domains, even though getDefinition/hasDefinition will return true for classes defined in parent domains. – Triynko Mar 31 '14 at 19:06
  • 1
    It is documented there but you need to set the player version to 11.3 or above to see it. Up the top under the page title. – Glen Blanchard May 15 '14 at 05:12
  • @GlenBlanchard +1 for introducing me to the filters! Thanks! – Tom Auger Jul 14 '14 at 20:15
10

EDIT: This is the quickest solution to your problem : http://www.bytearray.org/?p=175

Hi, you could use this library : https://github.com/claus/as3swf/wiki/ Don't have the time to do deeper test, but here is what i found :

1 - I have created a .swf containing in the library 2 exported MC, $Test and $Test2 2 - Once the .swf loaded by a Loader, i run this code :

var swf : SWF = new SWF(loader.contentLoaderInfo.bytes);
trace(swf);

3 - In the output you'll notice theses lines :

[76:SymbolClass] 
  Symbols:
    [0] TagID: 2, Name: $Test2
    [1] TagID: 1, Name: $Test

I think that there is a way to obtain this info directly thru the library API

OXMO456
  • 3,558
  • 2
  • 25
  • 35
0

You have to put the loaded SWF in the current ApplicationDomain.

Use ApplicationDomain.currentDomain to do that, on the ContextLoader info.

loader.load(request, new ContextLoader(false, ApplicationDomain.currentDomain));

It should work.

blue112
  • 52,634
  • 3
  • 45
  • 54
  • And then what? I have access to all classes, namespaces and functions of the loaded SWF? can I run: `var someSymbolSprite:Sprite = Sprite(new SomeSymbol())`? – Lucas Gabriel Sánchez Nov 10 '10 at 12:35
  • Yes, you can access every symbol in the loaded swf, just as if they was declared in the current movie. – blue112 Nov 10 '10 at 13:27
-1

Following from the answer I received from a previous question I asked here a few days ago (it's about SWC , but in your case, it doesn't really make a difference )
Working with SWCs - getDefinitionByName issue

If both SWFs share the same ApplicationDomain, you should be able to access the loaded SWF classes directly by doing this:

//provided that SomeSymbol extends Sprite...
var someSymbolSprite:Sprite =new SomeSymbol();

On the other hand, you won't be able to do this

var SomeSymbol:Class = getDefinitionByName("SomeSymbol");

unless you create a library of objects from the loaded SWF

var ssym:SomeSymbol;

Check the above link for more details.

Community
  • 1
  • 1
PatrickS
  • 9,539
  • 2
  • 27
  • 31