views:

49

answers:

4

We have a very large SWF hosted in a website which is just a repository of hundreds of movies/symbols. We load this SWF at the beginning with the Loader class but then the client will access a very few of them at that moment.

Is it possible to tell the loader to download symbols only when they're needed? (as with applicationDomain.getDefinition(...)).

Thanks in advance.

A: 

If it's just a repository of symbols, why don't split it?

Btw i'm sure that it is not possible, since to know where the symbol is (in bytes), you have to download the whole container.

kajyr
+1  A: 

You cannot load parts of a swf, however, you could generate several smaller swfs instead of the big one. For that to be doable you need to know how to include and exclude classes in swfs so you can build one or multiple base swfs that contain shared classes, and a whole bunch of 'extension' swfs that only contain a few classes and are using the shared classes from the base swfs.

This can be achieved by using the command-line options of mxmlc (the actionscript/mxml compiler shipped with the free flex sdk) -- I don't know if it is possible using the Flash IDE.

Of course this will make your loading a bit more complex, but it will save a lot of bandwidth.

Simon Groenewolt
+1  A: 

I don't think that is possible. However you can use JSFL to export all symbols from your library to separate SWF file's.

To do this, save the following as "ExportSymbols.jsfl"

//prompt user for directory to export symbols to
var dir = fl.browseForFolderURL('Select export folder');

//get all the library items
var symbols = fl.getDocumentDOM().library.items;

for ( var i=0; i<symbols.length; i++ ) {
    var symbol = symbols[i];
    var name = symbol.name;
    var linkageClassName = symbol.linkageClassName;
    if ( linkageClassName != "" ) {
        var filename = dir + "/" + linkageClassName + ".swf";
        symbol.exportSWF(filename);
        fl.trace("Exported '" + name + "' to '" + filename + "'");
    }
}

Then, in Flash choose Command > Run Command... and browse to the ExportSymbols.jsfl file.

That script will prompt you for an export directory and then will export all symbols that have a linkageClassName specified as separate SWF files.

Hope that helps.

maclema
+1  A: 

Don't think so, you might need to load the whole swf before using getDefinition().

A workaround I can think of is splitting the swf into many different ones.

Either naming the swf's by the class they contain, or storing some swf filename/contained classes pairs in a text file/xml and using that to load resources by name.

Have a look at BulkLoader, it could save you some time.

If you're using the Flash IDE, I wrote a simple jsfl script that should make things less tedious:

var doc   = fl.getDocumentDOM();
var dir   = 'file:///' + doc.path.substr(0,(doc.path.lastIndexOf('/')+1));
var lib   = doc.library;
var code  = 'var loader : BulkLoader = new BulkLoader("assets");\n';
var items = lib.items;
var iNum  = items.length;
var path;
var name;

fl.outputPanel.clear();

for(var i = 0 ; i < iNum ; i++){
    if(items[i].linkageExportForAS){
        name = items[i].linkageClassName;
        path = dir+name+'.swf';
        items[i].exportSWF(path);
        code += 'loader.add("'+(name+'.swf')+'", {id:"'+name+'"});\n';
    }
}

fl.clipCopyString(code);
fl.outputPanel.trace('symbols exported as swfs, code is in your clipboard');

All it does is loops thought the library items, and for the symbols that are exported for actionscript, it exports a swf file(where the Class name is used as a filename), and generates a bulk-loader snippet.

Easiest way to get it into Flash is this:

  1. Create a new Flash JavaScript File (File > New > Flash JavaScript File)
  2. Paste the code
  3. Save the file as ASSymbolsToFiles or something. It the file should default to the Commands folder, which means you could use it through the Commands menu. You can also set a keyboard shortcut, if needed.

The generated code might not be perfect, but it gives you an idea, and it should be readable enough for easy tweaks.

HTH, George

George Profenza